Find the Winner of the Circular Game

Sungju Kim·2024년 11월 28일
post-thumbnail


Theory: Josephus formula


Problem Context

In the Josephus problem:
1. (n) is the number of people in the circle.
2. (k) is the step size (how many people are skipped before someone is removed).
3. (J(n, k)) represents the position of the winner (last remaining person) in a circle of size (n), counting from 1.


Formula Explanation

The formula relates the winner's position in a circle of (n) people to the winner's position in a smaller circle of (n-1) people. Here's the step-by-step breakdown:

Step 1: Start with (J(n-1, k))

  • (J(n-1, k)) gives the position of the winner in a circle with (n-1) people.
  • This position is calculated relative to the smaller circle size.

Step 2: Add (k-1)

  • After removing a person, we move (k-1) steps forward to simulate skipping (k) people (the (k)-th person is removed).
  • The +(k-1) term adjusts the position to account for this "skipping."

Step 3: Take Modulo (n)

  • When adding (k-1), the resulting position might exceed the total number of people (n). To wrap the position back into the valid range ([1, n]), we use modulo (n):

Step 4: Convert to 1-Based Index by adding 1

  • Modulo arithmetic in programming is a 0-based index (i.e., starting at 0). Since the problem counts positions starting from 1, we add 1 to conver to 1-based indexing.

Proof of Theory

업로드중..

업로드중..

💡 Each iteration is effectively adding one person back into the "circle" and recalculating the position of the survivor as if the circle were growing.💡


Illustrative Example via Code solution

If the below solution is not intuitively comprehensive, try plugging in simple numbers for n and k, perhaps n=4, k=2.

Name0-Based Index (i)1-Based Index (i+1)
Stella01
Amy12
Baily23
Bob34
class Solution {
    public int findTheWinner(int n, int k) {
       int i=0;
       for(int j=2;j<=n;j++){
            i = (i+k)%j;
       }
        return i+1;
    }
}

Why does j start from 2?

j represents the number of people that are still playing the game. The game can only be played if there is more than one player because the last player that remains is the winner. So, our reverse calculation for the winner also starts with 2 not 1.


Time Complexity

The iterative approach runs in (O(n)), as it computes (J(n, k)) by looping through all circle sizes from 2 to (n).

profile
Fully ✨committed✨ developer, always eager to learn!

4개의 댓글

comment-user-thumbnail
2025년 2월 26일

First time hearing about this game. I read the conditions and didn’t get it. What’s the point of it? A lot of people ask me the same thing when they find out I play at Boomerang Casino https://canadaboomerang.com/ . The point of gambling is at least the chance to win and make some money. And while that’s not my main reason for playing, it’s always a nice bonus when luck’s on your side.

답글 달기
comment-user-thumbnail
2025년 6월 4일

Looking to find the winner of the circular game? Look no further than the ultimate platform for exciting strategy-based challenges. This addictive game pits players in a thrilling loop where only the smartest survive. Track moves, outwit opponents, and discover who will emerge victorious. At ufamax24.fun the fun never stops, and each round brings a new chance to win. Join today and test your skills in the ultimate circular showdown.

답글 달기
comment-user-thumbnail
2025년 6월 14일

Finding the winner of the circular game is a fun brain teaser—it’s all about patterns and smart elimination. If you're into games like that, you should check out https://familybelle.com/ too. It’s packed with exciting slot games, easy to join, and super entertaining—great for both fun and a chance to win big!

답글 달기
comment-user-thumbnail
2025년 7월 12일

Finding the winner of a circular game can be tricky but fun—it’s all about smart moves and timing. If you enjoy puzzles or strategic games, you’ll love exploring more at https://servicepompaairjogja.net/. They offer cool insights and entertainment options beyond just gameplay. Definitely worth checking out if you're into fun challenges and online excitement!

답글 달기