The problem
n friends sit in a circle; counting k from the current position eliminates a player, repeating until one remains (the Josephus problem). Return the winner (1-indexed).
Stuck? Reveal hints one at a time
How to approach it
- 1Work 0-indexed. The survivor of a 1-person game sits at position 0.
- 2For m = 2..n: survivor = (survivor + k) % m — undoing the renumbering that followed each elimination.
- 3Return survivor + 1 for the 1-indexed answer.
Key insight
After the first elimination, the remaining m−1 people form the SAME game shifted by k — so the smaller game’s answer maps forward with one modular addition.
The solution
Watch out for
- Mixing 0- and 1-indexing is the entire difficulty — do all math 0-indexed, convert once at the end.
- The queue simulation (rotate k−1, pop) is a fine fallback and easier to derive under pressure.