Find the Losers of the Circular Game - Problem
Imagine n friends sitting in a circle, numbered 1 to n clockwise. They're playing a special ball-passing game with an interesting twist!
Game Rules:
- Friend 1 starts with the ball
- On turn 1: Pass to friend
ksteps clockwise - On turn 2: Pass to friend
2×ksteps clockwise - On turn 3: Pass to friend
3×ksteps clockwise - And so on... on turn i, pass
i×ksteps clockwise
The game ends when someone receives the ball for the second time.
Your task: Find all the "losers" - friends who never got to touch the ball during the entire game. Return them in ascending order.
Note: Moving clockwise from friend n brings you back to friend 1 (circular array).
Input & Output
example_1.py — Basic Case
$
Input:
n = 5, k = 2
›
Output:
[4, 5]
💡 Note:
Game flow: Friend 1 starts → passes 2 steps to Friend 3 → Friend 3 passes 4 steps to Friend 2 → Friend 2 passes 6 steps (6%5=1) back to Friend 1. Friend 1 receives ball twice, so game ends. Friends 4 and 5 never received the ball.
example_2.py — Single Step
$
Input:
n = 4, k = 1
›
Output:
[]
💡 Note:
Game flow: Friend 1 → Friend 2 (1 step) → Friend 4 (2 steps) → Friend 3 (3 steps) → Friend 2 (4 steps). Friend 2 receives ball twice, game ends. All friends participated, so no losers.
example_3.py — Large Steps
$
Input:
n = 6, k = 3
›
Output:
[2, 3, 5, 6]
💡 Note:
Game flow: Friend 1 starts → passes 3 steps to Friend 4 → Friend 4 passes 6 steps (6%6=0, so back to Friend 4). Friend 4 receives ball twice immediately. Only friends 1 and 4 participated.
Constraints
- 2 ≤ n ≤ 50
- 1 ≤ k ≤ n
- The game will always end (mathematical guarantee)
- Note: Friends are numbered from 1 to n, not 0-indexed
Visualization
Tap to expand
Understanding the Visualization
1
Setup Circle
Friends 1-n sit in clockwise circle, friend 1 has ball
2
Escalating Steps
Each turn increases step size: k, 2k, 3k, etc.
3
Track Visited
Use hash set to remember who got the ball
4
Find Duplicate
Stop when someone receives ball second time
5
Identify Losers
Return friends who never got the ball
Key Takeaway
🎯 Key Insight: The game naturally creates a cycle, and we only need to simulate until we find the first repeat visitor. Hash sets make duplicate detection O(1), leading to optimal O(n) performance.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code