Skip to main content
Back to problems
#2682
Easy Algorithms

Find the losers of the circular game

Array Hash Table Simulation
49.8% acceptance
Feb 25, 2026
256
42
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. 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.

Solution

Rust
Time O(n)
Space O(n)
LeetCode
solution.rs
impl Solution {
  pub fn circular_game_losers(n: i32, k: i32) -> Vec<i32> {
    let n = n as usize;
    let mut visited = vec![false; n + 1];
    let mut pos = 1usize; // 1-indexed
    visited[pos] = true;
    let mut step = 1;
    loop {
      pos = (pos - 1 + step * k as usize) % n + 1;
      if visited[pos] { break; }
      visited[pos] = true;
      step += 1;
    }
    (1..=n).filter(|&i| !visited[i]).map(|i| i as i32).collect()
  }
}