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.k friends in the clockwise direction including the friend you started at. The counting wraps around the circle and may count some friends more than once.2 starting from the friend immediately clockwise of the friend who just lost and repeat.Given the number of friends, n, and an integer k, return the winner of the game.
Example 1:
Input: n = 5, k = 2 Output: 3 Explanation: Here are the steps of the game: 1) Start at friend 1. 2) Count 2 friends clockwise, which are friends 1 and 2. 3) Friend 2 leaves the circle. Next start is friend 3. 4) Count 2 friends clockwise, which are friends 3 and 4. 5) Friend 4 leaves the circle. Next start is friend 5. 6) Count 2 friends clockwise, which are friends 5 and 1. 7) Friend 1 leaves the circle. Next start is friend 3. 8) Count 2 friends clockwise, which are friends 3 and 5. 9) Friend 5 leaves the circle. Only friend 3 is left, so they are the winner.
Example 2:
Input: n = 6, k = 5 Output: 1 Explanation: The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.
Constraints:
1 <= k <= n <= 500
Follow up:
Could you solve this problem in linear time with constant space?
This approach simulates the game using a list (or array) to represent the friends. We start from the first friend and iteratively remove the k-th friend (considering the wrap around for the circle) until only one friend remains. The advantage of this method is its simplicity and straightforwardness, although it might not be the most efficient solution.
Steps:
This C code defines a function `findTheWinner` that constructs an array `friends` representing the circle and uses a loop to simulate the elimination process. The `index` keeps track of the current position, and friends are removed by shifting elements.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n^2), as each friend removal involves an O(n) operation in the worst case.
Space Complexity: O(n), due to the additional array used to represent friends.
The problem can be reduced to a well-known mathematical problem called the Josephus Problem. Instead of simulating each elimination, we derive the position of the winner directly using a mathematical formula.
The formula for the winner's position in the Josephus problem (0-indexed) is given by:
f(n) = (f(n-1) + k) % n for n > 1, and f(1) = 0 as the base case.
To get the result in 1-indexing that our problem requires, we add 1 to the result.
This C implementation uses the iterative formula for the Josephus Problem to find the winner efficiently without simulating each elimination.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n), iterating once for each friend.
Space Complexity: O(1), using a constant amount of space.
| Approach | Complexity |
|---|---|
| Approach 1: Simulating the Process with a List | Time Complexity: O(n^2), as each friend removal involves an O(n) operation in the worst case. |
| Approach 2: Mathematical Solution using Josephus Problem | Time Complexity: O(n), iterating once for each friend. |
Find the Winner of the Circular Game - Leetcode 1823 - Python • NeetCodeIO • 15,280 views views
Watch 9 more video solutions →Practice Find the Winner of the Circular Game with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor