# Josephus problem | Set 1 (A O(n) Solution)

In computer science and mathematics, the Josephus Problem (or Josephus permutation) is a theoretical problem. Following is the problem statement:

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.

For example, if n = 5 and k = 2, then the safe position is 3. Firstly, the person at position 2 is killed, then person at position 4 is killed, then person at position 1 is killed. Finally, the person at position 5 is killed. So the person at position 3 survives.

If n = 7 and k = 3, then the safe position is 4. The persons at positions 3, 6, 2, 7, 5, 1 are killed in order, and person at position 4 survives.

The problem has following recursive structure.

josephus(n, k) = (josephus(n - 1, k) + k-1) % n + 1 josephus(1, k) = 1

After the first person (kth from beginning) is killed, n-1 persons are left. So we call josephus(n – 1, k) to get the position with n-1 persons. But the position returned by josephus(n – 1, k) will consider the position starting from k%n + 1. So, we must make adjustments to the position returned by josephus(n – 1, k).

Following is simple recursive implementation of the Josephus problem. The implementation simply follows the recursive structure mentioned above.

## C++

`#include <iostream> ` `using` `namespace` `std; ` ` ` `int` `josephus(` `int` `n, ` `int` `k) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `/* The position returned by josephus(n - 1, k) ` ` ` `is adjusted because the recursive call ` ` ` `josephus(n - 1, k) considers the ` ` ` `original position k % n + 1 as position 1 */` ` ` `return` `(josephus(n - 1, k) + k-1) % n + 1; ` `} ` ` ` `// Driver Program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 14; ` ` ` `int` `k = 2; ` ` ` `cout << ` `"The chosen place is "` `<< josephus(n, k); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by shubhamsingh10 ` |

*chevron_right*

*filter_none*

## C

`#include <stdio.h> ` ` ` `int` `josephus(` `int` `n, ` `int` `k) ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `/* The position returned by josephus(n - 1, k) is adjusted because the ` ` ` `recursive call josephus(n - 1, k) considers the original position ` ` ` `k%n + 1 as position 1 */` ` ` `return` `(josephus(n - 1, k) + k-1) % n + 1; ` `} ` ` ` `// Driver Program to test above function ` `int` `main() ` `{ ` ` ` `int` `n = 14; ` ` ` `int` `k = 2; ` ` ` `printf` `(` `"The chosen place is %d"` `, josephus(n, k)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java code for Josephus Problem ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `static` `int` `josephus(` `int` `n, ` `int` `k) ` `{ ` `if` `(n == ` `1` `) ` ` ` `return` `1` `; ` `else` ` ` `/* The position returned by josephus(n - 1, k) ` ` ` `is adjusted because the recursive call ` ` ` `josephus(n - 1, k) considers the original ` ` ` `position k%n + 1 as position 1 */` ` ` `return` `(josephus(n - ` `1` `, k) + k-` `1` `) % n + ` `1` `; ` `} ` ` ` `// Driver Program to test above function ` `public` `static` `void` `main(String[] args) ` `{ ` `int` `n = ` `14` `; ` `int` `k = ` `2` `; ` `System.out.println(` `"The chosen place is "` `+ josephus(n, k)); ` `} ` `} ` ` ` `// This code is contributed by Prerna Saini ` |

*chevron_right*

*filter_none*

## Python3

`# Python code for Josephus Problem ` ` ` `def` `josephus(n, k): ` ` ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `1` ` ` `else` `: ` ` ` ` ` ` ` `# The position returned by ` ` ` `# josephus(n - 1, k) is adjusted ` ` ` `# because the recursive call ` ` ` `# josephus(n - 1, k) considers ` ` ` `# the original position ` ` ` `# k%n + 1 as position 1 ` ` ` `return` `(josephus(n ` `-` `1` `, k) ` `+` `k` `-` `1` `) ` `%` `n ` `+` `1` ` ` `# Driver Program to test above function ` ` ` `n ` `=` `14` `k ` `=` `2` ` ` `print` `(` `"The chosen place is "` `, josephus(n, k)) ` ` ` `# This code is contributed by ` `# Sumit Sadhakar ` |

*chevron_right*

*filter_none*

## C#

`// C# code for Josephus Problem ` `using` `System; ` ` ` `class` `GFG { ` ` ` ` ` `static` `int` `josephus(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `if` `(n == 1) ` ` ` `return` `1; ` ` ` `else` ` ` `/* The position returned ` ` ` `by josephus(n - 1, k) is ` ` ` `adjusted because the ` ` ` `recursive call josephus(n ` ` ` `- 1, k) considers the ` ` ` `original position k%n + 1 ` ` ` `as position 1 */` ` ` `return` `(josephus(n - 1, k) ` ` ` `+ k-1) % n + 1; ` ` ` `} ` ` ` ` ` `// Driver Program to test above ` ` ` `// function ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `int` `n = 14; ` ` ` `int` `k = 2; ` ` ` `Console.WriteLine(` `"The chosen "` ` ` `+ ` `"place is "` `+ josephus(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP code for ` `// Josephus Problem ` ` ` `function` `josephus(` `$n` `, ` `$k` `) ` `{ ` ` ` `if` `(` `$n` `== 1) ` ` ` `return` `1; ` ` ` `else` ` ` `/* The position returned by ` ` ` `josephus(n - 1, k) is ` ` ` `adjusted because the ` ` ` `recursive call josephus ` ` ` `(n - 1, k) considers the ` ` ` `original position k%n + 1 ` ` ` `as position 1 */` ` ` `return` `(josephus(` `$n` `- 1, ` `$k` `) + ` ` ` `$k` `- 1) % ` `$n` `+ 1; ` `} ` ` ` ` ` `// Driver Code ` ` ` `$n` `= 14; ` ` ` `$k` `= 2; ` ` ` `echo` `"The chosen place is "` `, josephus(` `$n` `, ` `$k` `); ` ` ` `// This code is contributed by ajit. ` `?> ` |

*chevron_right*

*filter_none*

Output:

The chosen place is 13

Time Complexity: O(n)

Josephus problem | Set 2 (A Simple Solution when k = 2)

Source:

http://en.wikipedia.org/wiki/Josephus_problem

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Josephus Problem Using Bit Magic
- Maximal Clique Problem | Recursive Solution
- Secretary Problem (A Optimal Stopping Problem)
- Find Cube Pairs | Set 2 (A n^(1/3) Solution)
- Find Cube Pairs | Set 1 (A n^(2/3) Solution)
- An interesting solution to get all prime numbers smaller than n
- Minimum number of jumps to reach end | Set 2 (O(n) solution)
- Compute nCr % p | Set 1 (Introduction and Dynamic Programming Solution)
- Recursive solution to count substrings with same first and last characters
- Find Two Missing Numbers | Set 2 (XOR based solution)
- Find the concentration of a solution using given Mass and Volume
- Number of n digit stepping numbers | Space optimized solution
- Lucky alive person in a circle | Code Solution to sword puzzle
- Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
- Transportation Problem | Set 7 ( Degeneracy in Transportation Problem )
- Fibonacci problem (Value of Fib(N)*Fib(N) - Fib(N-1) * Fib(N+1))
- Perfect Sum Problem
- 21 Matchsticks Problem
- Tiling Problem
- Water Jug Problem using Memoization