Leetcode 2682: Find the Losers of the Circular Game

grid47
grid47
Exploring patterns and algorithms
Feb 12, 2024 7 min read

In this game, there are n friends sitting in a circle, numbered from 1 to n. The game starts with the 1st friend receiving a ball. The ball is passed to a friend k steps away in a clockwise direction, and then on each subsequent turn, the ball is passed to the friend i * k steps away, where i is the turn number (starting from 1). The game ends when any friend receives the ball for the second time. The friends who never receive the ball are considered the losers. The task is to return the list of losers in ascending order.
Problem
Approach
Steps
Complexity
Input: You are given two integers, `n` and `k`, where `n` is the number of friends and `k` is the number of steps the ball is passed on each turn.
Example: Input: n = 6, k = 3
Constraints:
• 1 <= k <= n <= 50
Output: Return an array containing the losers of the game (the friends who never received the ball), sorted in ascending order.
Example: Output: [2, 4, 5, 6]
Constraints:
• The output should be an array of integers.
Goal: The goal is to simulate the passing of the ball based on the given rules and track which friends never receive the ball.
Steps:
• Step 1: Initialize an array to track whether each friend has received the ball.
• Step 2: Start with the 1st friend receiving the ball and pass it `k` steps away on each turn.
• Step 3: After each pass, mark the friend as having received the ball and continue passing it in increasing multiples of `k`.
• Step 4: Stop the game when any friend receives the ball for the second time.
• Step 5: Gather all the friends who never received the ball and return them as the losers.
Goal: The solution must handle cases efficiently even with the maximum values for `n` and `k`.
Steps:
• The ball passing process is repeated until a friend receives the ball a second time.
Assumptions:
• All input values are valid as per the problem constraints.
• The game will always end when a friend receives the ball for the second time.
Input: Input: n = 6, k = 3
Explanation: In this case, the ball starts with the 1st friend and is passed 3 steps away each time. The game proceeds as follows: 1st friend -> 4th friend -> 1st friend (game ends). The losers are the friends who never received the ball: 2nd, 5th, and 6th friends.

Input: Input: n = 4, k = 2
Explanation: The ball starts with the 1st friend and is passed 2 steps away each time. The game proceeds as follows: 1st friend -> 3rd friend -> 1st friend (game ends). The losers are the 2nd and 4th friends.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus