There are N Children are seated on N chairs arranged around a circle. The chairs are numbered from 1 to N. The game starts going in circles counting the children starting with the first chair. Once the count reaches K, that child leaves the game, removing his/her chair. The game starts again, beginning with the next chair in the circle. The last child remaining in the circle is the winner. Find the child that wins the game.

Examples:

Input : N = 5, K = 2 Output : 3 Firstly, the child at position 2 is out, then position 4 goes out, then position 1 Finally, the child at position 5 is out. So the position 3 survives. Input : 7 4 Output : 2

We have discussed a recursive solution for Josephus Problem . The given solution is better than the recursive solution of Josephus Solution which is not suitable for large inputs as it gives stack overflow. The time complexity is O(N).

**Approach** – In the algorithm, we use sum variable to find out the chair to be removed. The current chair position is calculated by adding the chair count K to the previous position i.e. sum and modulus of the sum. At last we return sum+1 as numbering starts from 1 to N.

## C++

`// Iterative solution for Josephus Problem ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function for finding the winning child. ` `long` `long` `int` `find(` `long` `long` `int` `n, ` `long` `long` `int` `k) ` `{ ` ` ` `long` `long` `int` `sum = 0, i; ` ` ` ` ` `// For finding out the removed ` ` ` `// chairs in each iteration ` ` ` `for` `(i = 2; i <= n; i++) ` ` ` `sum = (sum + k) % i; ` ` ` ` ` `return` `sum + 1; ` `} ` ` ` `// Driver function to find the winning child ` `int` `main() ` `{ ` ` ` `int` `n = 14, k = 2; ` ` ` `cout << find(n, k); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Iterative solution for Josephus Problem ` `class` `Test ` `{ ` ` ` ` ` `// Method for finding the winning child. ` ` ` `private` `int` `josephus(` `int` `n, ` `int` `k) ` ` ` `{ ` ` ` `int` `sum = ` `0` `; ` ` ` ` ` `// For finding out the removed ` ` ` `// chairs in each iteration ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` `sum = (sum + k) % i; ` ` ` `} ` ` ` ` ` `return` `sum+` `1` `; ` ` ` `} ` ` ` ` ` `// Driver Program to test above method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `int` `n = ` `14` `; ` ` ` `int` `k = ` `2` `; ` ` ` `Test obj = ` `new` `Test(); ` ` ` `System.out.println(obj.josephus(n, k)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Kumar Saras ` |

*chevron_right*

*filter_none*

**Output:**

13

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find four elements that sum to a given value | Set 1 (n^3 solution)
- Check if given Sudoku solution is valid or not
- Find an integral solution of the non-linear equation 2X + 5Y = N
- Eggs dropping puzzle (Binomial Coefficient and Binary Search Solution)
- C Program for Binary Search (Recursive and Iterative)
- QuickSelect (A Simple Iterative Implementation)
- Largest number less than or equal to N in BST (Iterative Approach)
- A Problem in Many Binary Search Implementations
- Pizza Problem
- Card Shuffle Problem | TCS Digital Advanced Coding Question
- 0/1 Knapsack Problem to print all possible solutions
- The painter's partition problem | Set 2
- The painter's partition problem
- Farthest index that can be reached from the Kth index of given array by given operations

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.