Find the Winner of the Circular Game
Medium
Topics
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
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.Input: a = 5, b = 2 Output: 3 Explanation: Friend 2 goes, then 4, then 1, then 5, leaving 3.
Example 2
Input: a = 6, b = 5 Output: 1 Explanation: Friend 5 goes first, then 4, then 6, then 2, then 3, leaving 1.
Example 3
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
- Simulation: put
1throughain a queue. While more than one remains, moveb - 1entries from the front to the back and then remove the front entry. Return what is left. - Recurrence: let
win(m)be the winner of the game withmfriends, numbered from 0 for the arithmetic. win(1) = 0, andwin(m) = (win(m - 1) + b) % m.- The shift by
bis because the surviving circle restartsbplaces along from the original start. - Return
win(a) + 1to 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
bfriends past the starting friend. The count includes the one you start on. - Moving
bentries to the back of the queue instead ofb - 1, which removes the wrong friend. - Returning a 0-based result. The friends are numbered from 1.
- Taking the modulus by
ainside the recurrence instead of by the current circle sizem. - Assuming
bis at mosta. 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 1import java.util.*;
class Solution {
public int findTheWinner(int a, int b) {
int win = 0;
for (int m = 2; m <= a; m++) {
win = (win + b) % m;
}
return win + 1;
}
}#include <bits/stdc++.h>
using namespace std;
class Solution {
public: int findTheWinner(int a, int b) {
int win = 0;
for (int m = 2; m <= a; m++) {
win = (win + b) % m;
}
return win + 1;
}
};/**
* @param {number} a
* @param {number} b
* @return {number}
*/ var findTheWinner = function(a, b) {
let win = 0;
for (let m = 2; m <= a; m++) {
win = (win + b) % m;
}
return win + 1;
};