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 m which indicates that m-1 persons are skipped and m-th 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.
Input : Length of circle : n = 4 Count to choose next : m = 2 Output : 1 Input : n = 5 m = 3 Output : 4
We have discussed different solutions of this problem (here and here). In this post a simple circular linked list based solution is discussed.
1) Create a circular linked list of size n.
2) Traverse through linked list and one by one delete every m-th node until there is one node left.
3) Return value of the only left node.
Last person left standing (Josephus Position) is 13
Time complexity: O(m * n)
This article is contributed by Raghav Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Convert singly linked list into circular linked list
- Check if a linked list is Circular Linked List
- Circular Linked List | Set 2 (Traversal)
- Sum of the nodes of a Circular Linked List
- Deletion from a Circular Linked List
- Reverse a circular linked list
- Deletion at different positions in a Circular Linked List
- Circular Linked List | Set 1 (Introduction and Applications)
- Reverse a doubly circular linked list
- Delete every Kth node from circular linked list
- Circular Singly Linked List | Insertion
- Exchange first and last nodes in Circular Linked List
- Doubly Circular Linked List | Set 2 (Deletion)
- Delete all the even nodes of a Circular Linked List