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 k steps clockwise
  • On turn 2: Pass to friend 2×k steps clockwise
  • On turn 3: Pass to friend 3×k steps clockwise
  • And so on... on turn i, pass i×k steps 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
Friend 11Friend 22Friend 33Friend 44Friend 55k=2 steps2k=4 steps3k=6 steps → Friend 1Game Progress✓ Turn 1: 1 → 3✓ Turn 2: 3 → 2→ Turn 3: 2 → 1 (REPEAT!)Losers: 4, 5
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.
Asked in
Google 15 Amazon 12 Meta 8 Microsoft 6
24.6K Views
Medium Frequency
~12 min Avg. Time
845 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen