Find the Winner of the Circular Game

n friends numbered 1 to n sit in a clockwise circle. Starting at friend 1, count k friends clockwise (including the starting friend); the last one counted leaves the circle. Counting then restarts at the friend just clockwise of the one who left. The last friend remaining wins. Return the winner.

30:00

Find the Winner of the Circular Game
medium
Free written solution
Topics
Companies

n friends numbered 1 to n sit in a clockwise circle. Starting at friend 1, count k friends clockwise (including the starting friend); the last one counted leaves the circle. Counting then restarts at the friend just clockwise of the one who left. The last friend remaining wins. Return the winner.

Example 1:
Input: {"n":5,"k":2}
Output: 3
Constraints:
  • 1≤k≤n≤5001 \leq k \leq n \leq 500

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"n":5,"k":2}

5 friends sit in a circle. Counting 2 clockwise from friend 1, the last one counted leaves.

the game, n = 5, k = 2

12345k = 2

blue ring = counting starts here · amber = counted · red = leaves

Variables
VariableValue
n5
k2
DepthFunction Call
Stack empty
0/11