The Josephus Problem
A classic elimination puzzle — people standing in a circle, every k-th one removed in turn — asking which position survives, and how to solve it with a simple recurrence instead of simulation.
Prerequisites: Induction and Recursive Structure in Puzzles
Picture people standing in a circle, numbered 1 through . Starting from person 1, you count off every -th person and remove them, continuing around the circle (skipping already-removed people) until only one survivor remains. The question: for given and , which starting position survives?
Simulating it directly — actually walking around a circle removing every -th person — works but is slow for large . The elegant approach uses a recurrence: if is the (0-indexed) surviving position among people counting by , then after the first removal you have people left, and the problem restarts on that smaller circle, just relabeled. The recurrence is and for . Each step solves the smaller problem, then maps its answer back onto the original numbering by shifting by positions and wrapping around with the modulus.
For example, with (every second person eliminated) and people: , , , , . So the 0-indexed survivor is position 2, meaning person 3 (1-indexed) survives — which matches direct simulation: eliminating 2, 4, 1, 5 in order leaves 3.
The Josephus problem's survivor position follows the recurrence starting from , letting you compute the answer in linear time instead of simulating the elimination round by round.
Related concepts
Practice in interviews
Further reading
- Graham, Knuth, Patashnik, Concrete Mathematics, ch. 1