Find the Winner of the Circular Game - Problem
There are n friends sitting in a circle, numbered from 1 to n in clockwise order. The game follows these rules:
- Start at friend 1
- Count k friends clockwise (including the current friend)
- The k-th friend is eliminated and leaves the circle
- Continue from the next friend clockwise until only one remains
Given n friends and counting number k, return the position of the winner (the last remaining friend).
Input & Output
Example 1 — Basic Case
$
Input:
n = 5, k = 2
›
Output:
3
💡 Note:
Start at friend 1, count 2 friends clockwise (1→2), eliminate friend 2. Continue from friend 3, count 2 (3→4), eliminate friend 4. Process continues until friend 3 remains as winner.
Example 2 — Single Friend
$
Input:
n = 1, k = 1
›
Output:
1
💡 Note:
Only one friend in the circle, so friend 1 automatically wins the game.
Example 3 — Large Step Size
$
Input:
n = 6, k = 5
›
Output:
1
💡 Note:
With 6 friends and k=5, we count 5 positions each time. The elimination pattern results in friend 1 being the last remaining.
Constraints
- 1 ≤ n ≤ 500
- 1 ≤ k ≤ n
Visualization
Tap to expand
Understanding the Visualization
1
Setup
n friends arranged in circle, numbered 1 to n
2
Eliminate
Count k friends clockwise, eliminate the k-th friend
3
Winner
Last remaining friend wins the game
Key Takeaway
🎯 Key Insight: This classic Josephus problem can be solved optimally using the mathematical recurrence relation J(n,k) = (J(n-1,k) + k) % n
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code