Josephus problem

Write codeMedium25 min

Your task

There are n people standing in a circle numbered 1 to n. Starting the count at person 1, you count k people and eliminate the k-th; counting then resumes from the next survivor. Return the 1-indexed position of the last person remaining. Constraints: 1 <= n <= 1e6, 1 <= k <= 1e6. An O(n) solution is expected (no O(n*k) circular-list simulation).

Examples

josephus(n: int, k: int) -> int
Example 1
Inputjosephus(5, 2)
Output3