Find the Losers of the Circular Game — LeetCode 2682 Python Solution

EasyArrayHash TableSimulation
Problem
#2682
Pattern
Hash Map
Reading time
2 min

The 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.

Example

Input
n = 5, k = 2
Output
[4,5]
Explanation
The game goes as follows:

Python solution

Python
class Solution:
    def circularGameLosers(self, n: int, k: int) -> List[int]:
        vis = [False] * n
        i, p = 0, 1
        while not vis[i]:
            vis[i] = True
            i = (i + p * k) % n
            p += 1
        return [i + 1 for i in range(n) if not vis[i]]

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n) auxiliary

Pattern: Hash Map

Trade memory for time: remember what you have seen so the second pass never happens. LeetCode 2682. Find the Losers of the Circular Game is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Hash Table.

The hash map guide has the Python template for the pattern and the 709 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2682. Find the Losers of the Circular Game?
LeetCode 2682. Find the Losers of the Circular Game is rated Easy on LeetCode.
What is the time complexity of LeetCode 2682. Find the Losers of the Circular Game?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2682. Find the Losers of the Circular Game?
The Python solution on this page uses O(n) auxiliary space.
What topics does LeetCode 2682. Find the Losers of the Circular Game cover?
LeetCode 2682. Find the Losers of the Circular Game is tagged Array, Hash Table and Simulation on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview