The problem can be solved recursively. If we know the position of the survivor for n-1 people, we can map it back to the circle of n people by shifting by k.
- Base case: If
n == 1, return 0 (0-indexed). - Recursive step:
survivor_pos = (josephus(n - 1, k) + k) % n. - Return
survivor_pos + 1for 1-indexed result.
- Time Complexity: O(N).
- Space Complexity: O(N) (stack).
def find_the_winner(n, k):
def solve(n, k):
if n == 1:
return 0
return (solve(n - 1, k) + k) % n
return solve(n, k) + 1