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 friend i brings you to friend i + 1 for 1 <= i < n, and moving clockwise from friend n brings you to friend 1.
The rules of the game are as follows:
-
Start at friend
1
.
-
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.
-
The last friend you counted leaves the circle and loses the game.
-
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.
-
Otherwise, the last friend in the circle wins the game.
Given the number of friends, n, and an integer k, return the label of the friend who wins the game.
Function Signature
def find_the_winner(n: int, k: int) -> int:
Constraints
Examples
Input: n = 5, k = 2
Output: 3
Start at 1 and count 1, 2: friend 2 leaves. Start at 3 and count 3, 4: friend 4 leaves. Start at 5 and count 5, 1: friend 1 leaves. Start at 3 and count 3, 5: friend 5 leaves, so friend 3 wins.
Input: n = 6, k = 5
Output: 1
Counting five friends each round removes them in the order 5, 4, 6, 2, 3. For example, the second round starts at 6 and counts 6, 1, 2, 3, 4, and the last round starts at 3 with only 1 and 3 left and counts 3, 1, 3, 1, 3. Friend 1 is the only one left.
Input: n = 4, k = 1
Output: 4
With k = 1 each round removes the friend the count starts at, so friends 1, 2 and 3 leave in turn and friend 4 wins.