This is an elimination game. Friends stand in a circle, numbered 1 to n. Starting from friend 1, we count k positions clockwise and eliminate the person at that position. Then we restart the count from the next person and continue until only one person remains. We return that last person.
This is the Josephus problem, a well-studied problem in combinatorics. After each elimination, the problem reduces to a smaller version of itself with a shifted starting position. That recursive structure leads to a mathematical solution that skips the simulation entirely.
1 <= k <= n <= 500 → With n at most 500, an O(n^2) simulation runs in at most about 250,000 operations, so it passes. The Josephus recurrence improves this to O(n) time and O(1) space.k can equal n, so the count may wrap all the way around the circle before eliminating someone.Do exactly what the problem says: arrange friends in a circle, count k positions, remove the person at that position, and repeat. We represent the circle as a list and use modular arithmetic to wrap around the end.
From a current position, counting k people forward (including the current one) lands on index (currentIndex + k - 1) % size. We remove that element. Because removal shifts every later element one slot to the left, the element that was directly after the removed one now sits at the same index we just removed from, which is where the next count should start. This mirrors the problem statement directly, so it is straightforward to verify.
(currentIndex + k - 1) % list.size().Loading animation...
This approach is correct but quadratic, since each removal from a list takes O(n). The next approach replaces the simulation with a formula that computes the winner's position directly.
After the first person is eliminated from a circle of n people, we are left with a circle of n-1 people, and the game continues from a shifted starting position. If we know the winner's position in the game of n-1 people, we can map it back to the original circle of n people. The Josephus recurrence does this mapping.
Define J(n) as the 0-indexed position of the winner in a circle of n people. The base case is J(1) = 0: with one person, that person wins. For larger n, the recurrence is J(n) = (J(n-1) + k) % n.
The first elimination removes the person at position (k-1) % n. The remaining n-1 people form a new, smaller circle whose own "position 0" is the survivor immediately after the eliminated one, namely old position k % n. Every position in the smaller game is therefore the old position minus k (mod n).
Inverting that relabeling: if the winner of the (n-1)-person game sits at position J(n-1) in the smaller circle, its position in the original n-person circle is (J(n-1) + k) % n. That is exactly the recurrence.
Loading animation...
The recursive approach runs in O(n) time but uses O(n) space for the recursion stack. Since each call depends only on the result of the call below it with no branching, the same chain can be computed iteratively from the bottom up, dropping the stack to O(1) space.
The recurrence is a straight chain: each value depends only on the one for n-1, with no branching and no memoization table. We can compute it from the bottom up instead of recursing top down.
Start with J(1) = 0. Then compute J(2), J(3), up to J(n), each time applying (previous + k) % currentSize. A single variable holds the running result, which is what brings the space down to O(1). The mathematics is identical to the recursive approach; only the direction of evaluation changes.
winner = 0 (the base case for 1 person).i from 2 to n:winner = (winner + k) % i.winner + 1 (convert from 0-indexed to 1-indexed).Loading animation...