Josephus Problem in Algorithm Design
Josephus Problem
What is the Josephus Problem?
The Josephus Problem is a theoretical problem related to a certain elimination game. It involves a group of people standing in a circle, where every k-th
person is eliminated until only one person remains. The challenge is to determine the position of the last person standing.
- n: The number of people in the circle.
- k: The number of steps to count before eliminating a person.
- The task is to find the position of the last remaining person after repeated elimination.
Josephus Problem: Recursive Solution
The Josephus problem can be solved recursively. The key observation is that once a person is eliminated, the problem size reduces, and the solution for n-1
people can be related to the solution for n
people.
Recursive Formula:
$$ Josephus(n, k) = (Josephus(n-1, k) + k) \% n $$
Where:
Josephus(n, k)
gives the position of the survivor forn
people and step sizek
.- The
+ k
accounts for the step size. - The modulo operation
% n
ensures the circular nature of the problem.
Josephus Problem: Implementation in Code
Here is a recursive implementation of the Josephus problem in C++:
#include <iostream>
using namespace std;
class Solution {
public:
int josephus(int n, int k) {
if (n == 1) return 0; // Base case: Only one person remains, position is 0 (0-based index)
return (josephus(n - 1, k) + k) % n;
}
};
int main() {
Solution sol;
int n = 5, k = 2; // Example: 5 people, every 2nd person is eliminated
cout << "The survivor's position is: " << sol.josephus(n, k) + 1 << endl; // Output in 1-based index
return 0;
}
Josephus Problem: Explanation of the Code {#josephus-code-explanation}
Base Case: When there’s only one person left
(n == 1)
, the survivor is the first person (position0
in0
-based index).Recursive Call: For
n > 1
, the recursive formula is applied to find the survivor’s position forn-1
people, then adjusted for the step size k using the modulo operation.Output: The result is printed as
josephus(n, k) + 1
to convert the0
-based result to a1
-based index.
Example Walkthrough {#josephus-example}
Let’s consider an example where n = 5
and k = 2
:
Step 1: Find the result for n = 1
- When
n = 1
, the survivor is at position0
.
Step 2: Find the result for n = 2
Recursively, we find the survivor for n = 1, which is 0.
The formula:
$$ (Josephus(1,2)+2) \% 2 = (0+2) \% 2 = 0 $$
So, the survivor for n = 2
is person 1
.
Step 3: Find the result for n = 3
Recursively, we find the survivor for
n = 2
, which is0
.The formula:
$$ (Josephus(2,2)+2) \% 3 = (0+2) \% 3 = 2 $$
So, the survivor for n = 3
is person 3
.
Step 4: Find the result for n = 4
Recursively, we find the survivor for n = 3, which is 2.
The formula:
$$ (Josephus(3,2)+2) \% 4 = (2+2) \% 4 = 0 $$
So, the survivor for n = 4
is person 1
.
Step 5: Find the result for n = 5
Recursively, we find the survivor for
n = 4
, which is0
.The formula:
$$ (Josephus(4,2)+2) \% 5 = (0+2) \% 5 = 2 $$
So, the survivor for n = 5
is person 3
.
Thus, the last person standing for n = 5
and k = 2
is person 3
(using 1
-based index).