Leetcode 1823: Find the Winner of the Circular Game

grid47
grid47
Exploring patterns and algorithms
May 8, 2024 5 min read

In this problem, there are n friends sitting in a circle. The game proceeds by counting k friends clockwise starting from the 1st friend. The last friend counted leaves the circle. This continues until only one friend remains, who is the winner. Your task is to find the winner of the game given n and k.
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 friends to be counted in each round.
Example: n = 5, k = 2
Constraints:
• 1 <= k <= n <= 500
Output: Return the number of the last remaining friend who wins the game.
Example: Output: 3
Constraints:
• The result is a single integer representing the winner's number.
Goal: To solve this problem, simulate the counting process and eliminate the friends until only one is left.
Steps:
• Initialize a list to represent the circle of friends.
• Repeat the elimination process: for each turn, count `k` friends clockwise, and remove the last counted friend.
• Continue until there is only one friend left in the circle, and return that friend as the winner.
Goal: Constraints on the input values.
Steps:
• 1 <= k <= n <= 500
Assumptions:
• The number of friends `n` is at least 1.
• The number of friends `n` is at most 500.
Input: n = 5, k = 2
Explanation: In this example, we start with friend 1 and eliminate friends in the order 2, 4, 1, and 5. Friend 3 is the last remaining and wins the game.

Input: n = 6, k = 5
Explanation: Here, we eliminate friends in the order 5, 4, 3, 2, and finally friend 1 wins as the last one remaining.

Link to LeetCode Lab


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