Find the Winner of the Circular Game (LeetCode 1823): Solution and Explanation

Medium
Array
Math
Recursion
Queue
Simulation

n friends sit in a circle. Starting from friend 1, you count k friends clockwise and the last one counted leaves, then counting restarts just after them. Return the number of the last friend left. This is the classic Josephus problem.

Intuition

The direct approach is to act out the game: keep the friends in a list or queue, step forward k - 1 places, remove that friend, and repeat until one remains. With n up to 500 that is fast enough at O(n * k), and it is a good way to check answers, but there is a neater idea.

Number the seats from 0, starting where the count begins. In a circle of i people, the first person to leave is seat (k - 1) % i, and the next round starts at seat k % i. What remains is just a smaller game with i - 1 people, whose seat 0 is our seat k % i.

So if you already know the winner's seat w in the game with i - 1 people, measured from that game's start, the same person sits at seat (w + k) % i in the bigger game. A circle of one person has its winner at seat 0, so you can build up from size 1 to size n with one line per size, then add 1 to turn the seat into a friend number.

Approach: Josephus recurrence

  1. Set winner = 0, the winner's seat in a circle of 1.
  2. For each size i from 2 to n, shift the smaller circle's answer by k seats: winner = (winner + k) % i.
  3. Seats are counted from friend 1, so return winner + 1.

Step-by-step example

n = 5, k = 2
  1. Size 1: winner = 0.
  2. Size 2: (0 + 2) % 2 = 0.
  3. Size 3: (0 + 2) % 3 = 2.
  4. Size 4: (2 + 2) % 4 = 0.
  5. Size 5: (0 + 2) % 5 = 2.
  6. Seat 2 is friend 3, so the function returns 3. Acting out the game agrees: friends 2, 4, 1 and 5 leave in that order.

Code: Python, Java, C++

1class Solution:
2    def findTheWinner(self, n: int, k: int) -> int:
3        winner = 0
4        for i in range(2, n + 1):
5            winner = (winner + k) % i
6        return winner + 1
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

The loop runs once for each circle size from 2 to n and does constant work each time.

Space

O(1)

Only the current winner seat is kept. The recursive form of the same formula would use O(n) stack space.

Other approaches

Simulate with a queue (time O(n * k), space O(n))

Put friends 1 to n in a queue. Each round, move k - 1 friends from the front to the back, then remove the front friend. When one friend remains, return it. Easy to get right and fine for n <= 500.

Simulate with a list and index (time O(n²), space O(n))

Keep an index into a list and remove the element at (index + k - 1) % size each round. Removal from an array list is O(n), so the total is O(n²), still fast enough here.

Recursive Josephus (time O(n), space O(n))

f(1) = 0 and f(i) = (f(i - 1) + k) % i, returning f(n) + 1. It is the same recurrence written top down and uses O(n) recursion depth.

Common mistakes

  • Mixing 0-based seats with 1-based friend numbers. The recurrence works on 0-based seats, so add 1 only at the very end.
  • Using (winner + k) % n instead of % i. Each step works in a circle of the current size i.
  • In a simulation, restarting the count at the removed friend instead of the friend after them, which shifts every later elimination.

Frequently asked questions

Why does (winner + k) % i work?

After seat (k - 1) % i leaves a circle of i, the rest is a circle of i - 1 that starts at seat k % i. A seat that is w places from that new start is at seat (w + k) % i in the old numbering, so the smaller game's winner maps to that seat.

Is this the Josephus problem?

Yes. It is the general Josephus problem with every k-th person removed, and the recurrence is the standard Josephus recurrence.

Is there a closed form?

For k = 2 there is: write n = 2^m + L with L < 2^m, and the winner is 2L + 1. For general k there is no simple closed form, which is why the O(n) recurrence is the standard answer.

What are the time and space complexity?

The iterative recurrence runs in O(n) time with O(1) space. A queue simulation takes O(n * k) time and O(n) space.