403. Josephus Problem Using Circular Linked List

HardLinked ListCircularMathRecursion

There are n people standing in a circle, numbered 1 to n. Starting from person 1, every k-th person is eliminated, continuing around the circle until one remains. Return the position (1-indexed) of the last survivor. Input: 'n, k'.

Input: 'n, k'.

Output: Integer — survivor position (1-indexed).

Examples

Example 1
Input: 7, 3
Output: 4
Explanation: Survivor is position 4.
Example 2
Input: 5, 2
Output: 3
Explanation: Survivor is position 3.
Example 3
Input: 1, 1
Output: 1
Explanation: Only one person.

Constraints

Asked by

AmazonGoogleMicrosoftMetaAdobe
Solve this problem in the editor →