Josephus Survivor
MediumMathRecursionSimulation
Description
n people stand in a circle numbered 1..n. Starting from person 1 and counting around, every k-th person is eliminated. Return the 1-indexed position of the last remaining person.
Examples
Input:
n = 5, k = 2Output:
3Explanation:
Classic Josephus, k=2 eliminates 2,4,1,5 leaving 3.
Input:
n = 1, k = 1Output:
1Explanation:
Only one person.
Input:
n = 7, k = 3Output:
4Explanation:
Standard Josephus walk.
Constraints
- •
1 ≤ n ≤ 10⁵ - •
1 ≤ k ≤ 10⁵