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: [3]
💡 Note: Ball passes: 1→2→1. Friends 1,2 receive the ball. Friend 3 never gets it.

Constraints

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

Visualization

Tap to expand
Circular Game - Find the Losers INPUT 1 2 3 4 5 Ball Start n = 5 friends k = 2 step size Clockwise direction Pass: i * k steps ALGORITHM STEPS 1 Initialize Track visits, start at 1 2 Simulate Passes Pass: (pos + i*k) % n 3 Stop Condition When someone gets 2nd ball 4 Find Losers Return unvisited friends Simulation Trace: Turn 1: 1 --[1*2]--> 3 Turn 2: 3 --[2*2]--> 2 Turn 3: 2 --[3*2]--> 3 STOP! 3 got ball twice Visited: {1, 2, 3} FINAL RESULT 1 2 3 4 5 Got ball Losers Output: [4, 5] Friends 4 and 5 never received ball OK Key Insight: Use modular arithmetic to handle circular movement: next_pos = (current + i * k) % n. Track visited friends with a boolean array. Game ends when any friend gets the ball twice. Time: O(n) - at most n passes before repeat. Space: O(n) for tracking visits. TutorialsPoint - Find the Losers of the Circular Game | Optimized Simulation Approach
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