The problem. n friends sit in a circle, numbered 1 to n clockwise. Starting at friend 1, count k friends clockwise, including the one you start on; the k-th friend leaves the circle. Counting starts again from the friend just after them. Repeat until one friend is left, and return their number. (This is the classic Josephus problem.)
n = 5, k = 2 -> 3 (2, 4, 1, 5 leave in that order)
n = 6, k = 5 -> 1
n = 4, k = 1 -> 4 (everyone leaves in order; the last one wins)Members only · free account
The rest of this lesson, with 7 more sections, 1 drawing, a dry-run simulator and code in JavaScript, Python, Java and C++, opens with a free account.
Was this helpful?
Not ready yet? The Arrays course is free to read, no account needed.
In the rest of this lesson