Taro Logo

Find the Winner of the Circular Game

Medium
23 days ago

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 i<sup>th</sup> friend brings you to the (i+1)<sup>th</sup> friend for 1 <= i < n, and moving clockwise from the n<sup>th</sup> friend brings you to the 1<sup>st</sup> friend.

The rules of the game are as follows:

  1. Start at the 1<sup>st</sup> friend.
  2. Count the next 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.
  3. The last friend you counted leaves the circle and loses the game.
  4. If there is still more than one friend in the circle, go back to step 2 starting from the friend immediately clockwise of the friend who just lost and repeat.
  5. Else, the last friend in the circle wins the game.

Given the number of friends, n, and an integer k, return the winner of the game.

For example, if n = 5 and k = 2, the output should be 3. Here's how the game unfolds:

  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.

Another example: if n = 6 and k = 5, the output should be 1. The friends leave in this order: 5, 4, 6, 2, 3. The winner is friend 1.

Sample Answer

The problem describes a game where n friends are arranged in a circle, and every kth friend is eliminated until only one winner remains. The task is to determine the winner.

Naive Approach

A straightforward approach would be to simulate the game using a list or array to represent the circle of friends. We iterate through the list, removing friends according to the rules until only one friend remains. This method is easy to understand but can be inefficient for larger values of n and k.

def find_the_winner_naive(n: int, k: int) -> int:
    friends = list(range(1, n + 1))
    current_index = 0

    while len(friends) > 1:
        current_index = (current_index + k - 1) % len(friends)
        friends.pop(current_index)

    return friends[0]


# Example usage:
n = 5
k = 2
print(find_the_winner_naive(n, k))
# Output: 3

n = 6
k = 5
print(find_the_winner_naive(n, k))
# Output: 1

Optimal Solution (Josephus Problem)

The problem is a variation of the Josephus Problem. The Josephus Problem has a recursive solution that can be implemented iteratively for better performance. The recursive relation is:

J(1, k) = 1 J(n, k) = (J(n - 1, k) + k - 1) % n + 1

This formula calculates the position of the winner directly without simulating the elimination process.

def find_the_winner(n: int, k: int) -> int:
    winner = 1
    for i in range(2, n + 1):
        winner = (winner + k - 1) % i + 1
    return winner


# Example usage:
n = 5
k = 2
print(find_the_winner(n, k))
# Output: 3

n = 6
k = 5
print(find_the_winner(n, k))
# Output: 1

Big(O) Run-time Analysis

  • Naive Approach: The time complexity is O(n*k) in the worst case. We potentially iterate through the list of friends multiple times, and each pop operation takes O(n) time.
  • Optimal Solution: The optimal solution has a time complexity of O(n). We iterate from 2 to n once, performing constant time operations within the loop.

Big(O) Space Usage Analysis

  • Naive Approach: The space complexity is O(n) because we store the list of friends.
  • Optimal Solution: The space complexity is O(1) because we only use a constant amount of extra space to store the winner variable, irrespective of the input size.

Edge Cases

  1. n = 1: If there is only one friend, that friend is the winner.
  2. k = 1: If k is 1, the friends are eliminated in order (1, 2, 3, ...).
  3. k > n: The counting wraps around the circle, so the modulo operation % handles cases where k is larger than n.
  4. Large n and k: Optimal solution uses iterative approach which avoids stack overflow issues that may occur when using recursion for very large inputs.

All these cases are handled correctly by the provided optimal solution. The iterative implementation also prevents stack overflow errors that might occur with a recursive approach for very large inputs.