Find the Winner of the Circular Game

Given n friends playing a circular elimination game and a step size k, return the winner. Pattern focus: Queue Simulation. Simulate the circular process until one person remains.

Input Format

n = number of players, k = elimination step

Output Format

winner's label

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • n, k must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

n = 5
k = 2

Output:

3

Explanation:

After repeated eliminations, player 3 remains.

Example 2:

Input:

n = 6
k = 5

Output:

1

Explanation:

The elimination order leaves player 1 as the winner.

Loading...
Find the Winner of the Circular Game