Josephus problem | Set 2 (A Simple Solution when k = 2)
There are n people standing in a circle waiting to be executed. The counting out begins at some point in the circle and proceeds around the circle in a fixed direction. In each step, a certain number of people are skipped and the next person is executed. The elimination proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom. Given the total number of persons n and a number k which indicates that k-1 persons are skipped and kth person is killed in circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive.
We have discussed a generalized solution in below set 1.
Josephus problem | Set 1 (A O(n) Solution)
In this post, a special case is discussed when k = 2
Examples :
Input : n = 5 Output : The person at position 3 survives Explanation : Firstly, the person at position 2 is killed, then at 4, then at 1 is killed. Finally, the person at position 5 is killed. So the person at position 3 survives. Input : n = 14 Output : The person at position 13 survives
Below are some interesting facts.
- In first round all even positioned persons are killed.
- For second round two cases arise
- If n is even : For example n = 8. In first round, first 2 is killed, then 4, then 6, then 8. In second round, we have 1, 3, 5 and 7 in positions 1st, 2nd, 3rd and 4th respectively.
- If n is odd : For example n = 7. In first round, first 2 is killed, then 4, then 6. In second round, we have 3, 5, 7 in positions 1st, 2nd and 3rd respectively.
If n is even and a person is in position x in current round, then the person was in position 2x – 1 in previous round.
If n is odd and a person is in position x in current round, then the person was in position 2x + 1 in previous round.
From above facts, we can recursively define the formula for finding position of survivor.
Let f(n) be position of survivor for input n, the value of f(n) can be recursively written as below. If n is even f(n) = 2f(n/2) - 1 Else f(n) = 2f((n-1)/2) + 1
Solution of above recurrence is
f(n) = 2(n - 2floor(Log2n) + 1
= 2n - 21 + floor(Log2n) + 1
Below is the implementation to find value of above formula.
C++
// C/C++ program to find solution of Josephus // problem when size of step is 2. #include <stdio.h> // Returns position of survivor among a circle // of n persons and every second person being // killed int josephus(int n) { // Find value of 2 ^ (1 + floor(Log n)) // which is a power of 2 whose value // is just above n. int p = 1; while (p <= n) p *= 2; // Return 2n - 2^(1+floor(Logn)) + 1 return (2*n) - p + 1; } // Driver Program to test above function int main() { int n = 16; printf("The chosen place is %d", josephus(n)); return 0; } |
Java
// Java program to find solution of Josephus // problem when size of step is 2. import java.io.*; class GFG { // Returns position of survivor among // a circle of n persons and every // second person being killed static int josephus(int n) { // Find value of 2 ^ (1 + floor(Log n)) // which is a power of 2 whose value // is just above n. int p = 1; while (p <= n) p *= 2; // Return 2n - 2^(1+floor(Logn)) + 1 return (2*n) - p + 1; } // Driver Program to test above function public static void main(String[] args) { int n = 16; System.out.println("The chosen place is " + josephus(n)); } } // This Code is Contributed by Anuj_67 |
Python3
# Python3 program to find solution of # Josephus problem when size of step is 2. # Returns position of survivor among a # circle of n persons and every second # person being killed def josephus(n): # Find value of 2 ^ (1 + floor(Log n)) # which is a power of 2 whose value # is just above n. p = 1 while p <= n: p *= 2 # Return 2n - 2^(1 + floor(Logn)) + 1 return (2 * n) - p + 1 # Driver Code n = 16print ("The chosen place is", josephus(n)) # This code is contributed by Shreyanshi Arun. |
C#
// C# program to find solution of Josephus // problem when size of step is 2. using System; class GFG { // Returns position of survivor among // a circle of n persons and every // second person being killed static int josephus(int n) { // Find value of 2 ^ (1 + floor(Log n)) // which is a power of 2 whose value // is just above n. int p = 1; while (p <= n) p *= 2; // Return 2n - 2^(1+floor(Logn)) + 1 return (2*n) - p + 1; } // Driver Program to test above function static void Main() { int n = 16; Console.Write("The chosen place is " + josephus(n)); } } // This Code is Contributed by Anuj_67 |
PHP
<?php // PHP program to find solution // of Josephus problem when // size of step is 2. // Returns position of survivor // among a circle of n persons // and every second person being // killed function josephus($n) { // Find value of 2 ^ (1 + floor(Log n)) // which is a power of 2 whose value // is just above n. $p = 1; while ($p <= $n) $p *= 2; // Return 2n - 2^(1+floor(Logn)) + 1 return (2 * $n) - $p + 1; } // Driver Code $n = 16; echo "The chosen place is ", josephus($n); // This code is contributed by ajit. ?> |
Output :
The chosen place is 1
Time complexity of above solution is O(Log n).
This article is contributed by Rahul Jain. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Shortest Superstring Problem | Set 2 (Using Set Cover)
- Exact Cover Problem and Algorithm X | Set 1
- Exact Cover Problem and Algorithm X | Set 2 (Implementation with DLX)
- Maths behind number of paths in matrix problem
- Prim's Algorithm (Simple Implementation for Adjacency Matrix Representation)
- Program to solve the Alligation Problem
- Travelling Salesman Problem implementation using BackTracking
- Find maximum path sum in a 2D matrix when exactly two left moves are allowed
- Factorial of an Array of integers
- Find the number of Chicks in a Zoo at Nth day
- Count the nodes of the tree which make a pangram when concatenated with the sub-tree nodes
- Algorithms Sample Questions | Set 3 | Time Order Analysis
- Program to Calculate e^x by Recursion
- Algorithms Sample Questions | Recurrences | Set 2



