Josephus Problem when k is 2
Last Updated :
02 Mar, 2023
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++
#include <stdio.h>
int josephus( int n)
{
int p = 1;
while (p <= n)
p *= 2;
return (2 * n) - p + 1;
}
int main()
{
int n = 16;
printf ( "The chosen place is %d" , josephus(n));
return 0;
}
|
Java
import java.io.*;
class GFG {
static int josephus( int n)
{
int p = 1 ;
while (p <= n)
p *= 2 ;
return ( 2 * n) - p + 1 ;
}
public static void main(String[] args)
{
int n = 16 ;
System.out.println( "The chosen place is "
+ josephus(n));
}
}
|
Python3
def josephus(n):
p = 1
while p < = n:
p * = 2
return ( 2 * n) - p + 1
n = 16
print ( "The chosen place is" , josephus(n))
|
C#
using System;
class GFG {
static int josephus( int n)
{
int p = 1;
while (p <= n)
p *= 2;
return (2 * n) - p + 1;
}
static void Main()
{
int n = 16;
Console.Write( "The chosen place is "
+ josephus(n));
}
}
|
PHP
<?php
function josephus( $n )
{
$p = 1;
while ( $p <= $n )
$p *= 2;
return (2 * $n ) - $p + 1;
}
$n = 16;
echo "The chosen place is " , josephus( $n );
?>
|
Javascript
<script>
function josephus(n)
{
let p = 1;
while (p <= n)
p *= 2;
return (2 * n) - p + 1;
}
let n = 16;
document.write( "The chosen place is " +
josephus(n));
</script>
|
Output:
The chosen place is 1
Time complexity of above solution is O(Log n).
An another interesting solution to the problem while k=2 can be given based on an observation, that we just have to left rotate the binary representation of N to get the required answer. A working code for
the same is provided below considering number to be 64-bit number.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int josephus( int n)
{
if (!(n & (n - 1)) && n) {
return 1;
}
bitset<64> Arr(n);
bool f = false ;
for ( int i = 63; i >= 0; --i) {
if (Arr[i] == 1 && !f) {
f = true ;
Arr[i] = Arr[i - 1];
}
if (f) {
Arr[i] = Arr[i - 1];
}
}
Arr[0] = 1;
int res;
res = ( int )(Arr.to_ulong());
return res;
}
int main()
{
int n = 16;
printf ( "The chosen place is %d" , josephus(n));
return 0;
}
|
Java
public class Main {
public static int josephus( int n)
{
if (~((n & (n - 1 ))) != 0 && n != 0 ) {
return 1 ;
}
int [] arr = new int [ 64 ];
char [] bin = Integer.toBinaryString(n).toCharArray();
int i = 64 - bin.length;
for ( int j = 0 ; j < bin.length; j++) {
arr[i++] = bin[j] - '0' ;
}
boolean f = false ;
for (i = 63 ; i >= 0 ; i--) {
if (arr[i] == 1 && !f) {
f = true ;
arr[i] = arr[i - 1 ];
}
if (f) {
arr[i] = arr[i - 1 ];
}
}
arr[ 0 ] = 1 ;
int res = 0 ;
for (i = 0 ; i < 64 ; i++) {
res += arr[i] * (1L << ( 63 - i));
}
return res;
}
public static void main(String[] args) {
int n = 16 ;
System.out.println( "The chosen place is " + josephus(n));
}
}
|
Python3
def josephus(n):
if (~(n & (n - 1 )) and n) :
return 1
Arr = list ( map ( lambda x: int (x), list ( bin (n)[ 2 :])))
Arr = [ 0 ] * ( 64 - len (Arr)) + Arr
f = False
for i in range ( 63 , - 1 , - 1 ) :
if (Arr[i] = = 1 and not f) :
f = True
Arr[i] = Arr[i - 1 ]
if (f) :
Arr[i] = Arr[i - 1 ]
Arr[ 0 ] = 1
res = int (''.join(Arr), 2 )
return res
if __name__ = = '__main__' :
n = 16
print ( "The chosen place is" , josephus(n))
|
C#
using System;
public class Mainn
{
public static long josephus( long n)
{
if (~((n & (n - 1))) != 0 && n != 0)
{
return 1;
}
int [] arr = new int [64];
char [] bin = Convert.ToString(n, 2).ToCharArray();
int i = 64 - bin.Length;
for ( int j = 0; j < bin.Length; j++)
{
arr[i++] = bin[j] - '0' ;
}
bool f = false ;
for (i = 63; i >= 0; i--)
{
if (arr[i] == 1 && !f)
{
f = true ;
arr[i] = arr[i - 1];
}
if (f)
{
arr[i] = arr[i - 1];
}
}
arr[0] = 1;
long res = 0;
for (i = 0; i < 64; i++)
{
res += arr[i] * (1L << (63 - i));
}
return res;
}
public static void Main( string [] args)
{
long n = 16;
Console.WriteLine( "The chosen place is " + josephus(n));
}
}
|
Javascript
function josephus(n) {
if (~(n & (n - 1)) && n) {
return 1;
}
let Arr = n.toString(2).split( '' ).map(x => parseInt(x));
Arr = Array(64 - Arr.length).fill(0).concat(Arr);
let f = false ;
for (let i = 63; i >= 0; i--) {
if (Arr[i] == 1 && !f) {
f = true ;
Arr[i] = Arr[i - 1];
}
if (f) {
Arr[i] = Arr[i - 1];
}
}
Arr[0] = 1;
let res = parseInt(Arr.join( '' ), 2);
return res;
}
let n = 16;
console.log( "The chosen place is" , josephus(n));
|
Output:
The chosen place is 1
Time Complexity : O(log(n))
Auxiliary Space: O(log(n))
This idea is contributed by Anukul Chand.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...