Loading...
Loading...
Loading...
n people stand in a circle numbered 1 to n. Starting at person 1, every kth person is removed, going around the circle, until one person is left. Find that person. Use the recursion J(1) = 0 and J(n) = (J(n - 1) + k) % n, where J is a 0-based position.
Example
Input
7 3
Output
4
People are removed in the order 3, 6, 2, 7, 5, 1.
Try the problem first. Open one hint at a time.
With one person left the survivor sits at position 0.
A circle of n people becomes a circle of n - 1 after one removal, with positions shifted by k.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor