Find the Losers of the Circular Game
Let’s break down this statement.
“We prepopulate the result array with all numbers”: This means that initially, we create an array and fill it with all the possible numbers. If we are discussing the context of the ‘circular game’, these numbers would represent all the friends. Each friend is represented by a distinct number in this array.
“and set a number to zero once visited.”: In the context of the ‘circular game’, a friend “visits” when they receive the ball. So, each time a friend gets the ball, we mark their corresponding number in the array as zero. This helps keep track of which friends have already received the ball.
“After the simulation, we return remaining nonzero numbers.”: After the game is over, we are interested in the friends who never got the ball. These are the friends whose corresponding numbers in the array were not set to zero (since they never received the ball). So, we look at our array and return the numbers that are still nonzero. These nonzero numbers represent the friends who never got the ball  the losers of the game.
We are using an array to keep track of who received the ball in the game. We initially assume that everyone will receive the ball and as the game progresses, we mark off those who do get the ball. At the end, we return the friends who never received the ball, hence they are the losers of the game.


Here’s a detailed breakdown of how this function works:
It creates a set
s
containing all integers from 1 ton
inclusive. In Python, a set is a collection of unique elements.It generates a sequence of numbers by successively adding numbers from 0 to
n1
. Theaccumulate
function from Python’sitertools
module is used for this.For each number
mul
in the generated sequence, it calculates the number to be removed from the sets
. This is done by multiplyingmul
byk
and then taking the remainder after dividing byn
. The result is incremented by 1 because the numbers in the sets
start from 1.If the number to be removed is not in the set
s
, it breaks the loop and stops removing numbers.If the number to be removed is in the set
s
, it removes the number from the set.Finally, it returns the set
s
, which contains the numbers that have not been removed.
This function has a time complexity of O(n), where n
is the input parameter, because it iterates over a sequence of length n
. It has a space complexity of O(n) because it creates a set and a list, both of length n
.
Here’s a detailed breakdown of the Python solution:


This code defines a class Solution
with a method circularGameLosers
, which takes two parameters, n
and k
. Inside the method, a set s
is created that contains integers from 1 to n
inclusive.


The accumulate
function generates a list where each element is the sum of the current and all previous elements in the range from 0 to n1
. The for
loop iterates over each element in this list.


This code calculates the product of k
and mul
(the current element from the accumulated list), then takes the modulus by n
and increments it by 1. If the resulting number is not in the set s
, the loop is broken and we stop removing numbers.


If the calculated number is in the set s
, this line of code removes it from the set.


Finally, the method returns the set s
, which contains the remaining numbers after the removal process.
This function simulates a circular game, where players numbered 1 to n
are removed from the game in a certain order, determined by k
and mul
. The remaining players (those who were not removed) are returned as the output.
Beginner Friendly Version


In this code, we first initialize a set containing all the friends. Then we create a list of multiples which we are going to use in the game process. The accumulate
function returns accumulated sums. We then start the game and for each accumulated multiple, we calculate the index of the friend who gets the ball and remove them from the set if they are still in the game (i.e., they are in the set). The game ends when the ball reaches a friend who is already out of the game (i.e., they are not in the set). Finally, we return the list of losers which are the friends remaining in the set.