Find the Winner of the Circular Game

Medium

Topics
ArrayMathRecursionQueueSimulation

a friends sit in a circle, numbered 1 to a clockwise. Starting at friend 1, count b friends clockwise including the one you start on; that friend leaves the circle. Counting then resumes from the next friend still in it.

Return the number of the last friend remaining.

LeetCode names the arguments n and k. The harness here supplies two integers called a and b: a is the number of friends and b is the count.

Example 1

12345
the last one remaining
With 5 friends and a count of 2, friend 3 is the last one left. The labels give the order people leave. Counting starts at friend 1 and includes the friend you start on, so with b = 1 the person you are standing on is the one who goes. After each removal the count resumes from the next friend still in the circle, not from friend 1 again.
Friends are numbered from 1 clockwise, and the answer is a number rather than a position.
Input:  a = 5, b = 2
Output: 3
Explanation: Friend 2 goes, then 4, then 1, then 5, leaving 3.

Example 2

123456
the last one remaining
Input:  a = 6, b = 5
Output: 1
Explanation: Friend 5 goes first, then 4, then 6, then 2, then 3, leaving 1.

Example 3

1
the last one remaining
With a single friend nobody is ever removed, so they win by default.
Input:  a = 1, b = 1
Output: 1
Explanation: One friend, who is already the last one standing.

Constraints

  • 1 <= a <= 500
  • 1 <= b <= 500
Editorial — intuition, approach, complexity and pitfalls

Intuition

Acting it out is entirely reasonable at these limits: keep the friends in a ring, walk b steps, remove, repeat. The thing to get right is the off-by-one in the counting, because the count includes the friend you start on. From friend 1 with b = 2, the friend who leaves is 2 and not 3.

A rotating queue expresses that cleanly without any index arithmetic: move b - 1 friends from the front to the back, then discard the one now at the front. When a single friend is left, that is the answer.

There is also a recurrence, and it is worth understanding because it removes the simulation entirely. After the first friend is eliminated, what remains is the same game with one fewer player — just numbered from a different starting point.

Solve the smaller game, then translate its answer back. If the smaller game’s winner is at position w counting from its own start, then in the original circle that position sits b places further on, so the original answer is (w + b) % a, with the zero case mapping to a because the friends are numbered from 1.

Approach

  1. Simulation: put 1 through a in a queue. While more than one remains, move b - 1 entries from the front to the back and then remove the front entry. Return what is left.
  2. Recurrence: let win(m) be the winner of the game with m friends, numbered from 0 for the arithmetic.
  3. win(1) = 0, and win(m) = (win(m - 1) + b) % m.
  4. The shift by b is because the surviving circle restarts b places along from the original start.
  5. Return win(a) + 1 to get back to numbering from 1.

Walkthrough

a = 5, b = 2

circle 1 2 3 4 5, start at 1

   count 2 from 1, INCLUDING 1:  1, 2   -> 2 leaves
   remaining 1 3 4 5, resume at 3
   count 2 from 3:  3, 4                -> 4 leaves
   remaining 1 3 5, resume at 5
   count 2 from 5:  5, 1                -> 1 leaves
   remaining 3 5, resume at 3
   count 2 from 3:  3, 5                -> 5 leaves
   remaining 3

answer 3

the same thing via the recurrence, numbering from 0
   win(1) = 0
   win(2) = (0 + 2) % 2 = 0
   win(3) = (0 + 2) % 3 = 2
   win(4) = (2 + 2) % 4 = 0
   win(5) = (0 + 2) % 5 = 2
   answer 2 + 1 = 3

the off-by-one to watch
   "count b friends including the one you start on" means
   b = 1 eliminates the friend you are standing on, so the
   friends go 1, 2, 3, ... and the last one wins. A solution
   that counts b friends PAST the start is solving a
   different problem.

Complexity

  • Time — O(a): one step of the recurrence per friend; the simulation is O(a * b).
  • Space — O(1): a single accumulator; the simulation needs a queue of a entries.

Common Pitfalls

  • Counting b friends past the starting friend. The count includes the one you start on.
  • Moving b entries to the back of the queue instead of b - 1, which removes the wrong friend.
  • Returning a 0-based result. The friends are numbered from 1.
  • Taking the modulus by a inside the recurrence instead of by the current circle size m.
  • Assuming b is at most a. It can be larger, and the counting simply wraps.

Full Solution — all languages

class Solution:
    def findTheWinner(self, a, b):
        # win(m) is the winner of the m-friend game, numbered from 0.
        # The surviving circle restarts b places along, hence the shift.
        win = 0

        for m in range(2, a + 1):
            win = (win + b) % m

        return win + 1        # back to numbering from 1
SensAI
Beta
Tap the mic to stop voice mode