Find the Losers of the Circular Game - Problem

There are n friends that are playing a game. The friends are sitting in a circle and are numbered from 1 to n in clockwise order. More formally, moving clockwise from the ith friend brings you to the (i+1)th friend for 1 <= i < n, and moving clockwise from the nth friend brings you to the 1st friend.

The rules of the game are as follows:

  • 1st friend receives the ball.
  • After that, 1st friend passes it to the friend who is k steps away from them in the clockwise direction.
  • After that, the friend who receives the ball should pass it to the friend who is 2 * k steps away from them in the clockwise direction.
  • After that, the friend who receives the ball should pass it to the friend who is 3 * k steps away from them in the clockwise direction, and so on and so forth.

In other words, on the ith turn, the friend holding the ball should pass it to the friend who is i * k steps away from them in the clockwise direction.

The game is finished when some friend receives the ball for the second time.

The losers of the game are friends who did not receive the ball in the entire game.

Given the number of friends, n, and an integer k, return the array answer, which contains the losers of the game in the ascending order.

Input & Output

Example 1 — Basic Case
$ Input: n = 5, k = 2
Output: [4,5]
💡 Note: Ball passes: 1→3→2→1. Friends 1,2,3 received ball. Friends 4,5 never got it.
Example 2 — Smaller Circle
$ Input: n = 4, k = 4
Output: [2,3,4]
💡 Note: Ball passes: 1→1 (4 steps in circle of 4 returns to 1). Only friend 1 gets the ball.
Example 3 — All Get Ball
$ Input: n = 3, k = 1
Output: []
💡 Note: Ball passes: 1→2→3→1. All friends receive the ball, so no losers.

Constraints

  • 1 ≤ n ≤ 50
  • 1 ≤ k ≤ n

Visualization

Tap to expand
Circular Game: Find the Losers (n=5, k=2)12345Ball Path: 1 → 3 → 2 → 1Received: Friends 1, 2, 3Losers: Friends 4, 5Simulation StepsTurn 1: Friend 1 has ballTurn 2: Move 2*1=2 steps → Friend 3Turn 3: Move 2*2=4 steps → Friend 2Turn 4: Move 2*3=6 steps → Friend 1Friend 1 gets ball again - STOPNever received: [4, 5]Output: [4, 5]
Understanding the Visualization
1
Input
n=5 friends in circle, k=2 step size
2
Process
Simulate ball passing until someone gets it twice
3
Output
Friends who never received the ball
Key Takeaway
🎯 Key Insight: Simulate the game rules exactly and track which friends never receive the ball using a set or boolean array
Asked in
Amazon 15 Microsoft 10
12.0K Views
Medium Frequency
~15 min Avg. Time
450 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