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 firstname.lastname@example.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.
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.
- Circular Queue | Set 2 (Circular Linked List Implementation)
- Check if a linked list is Circular Linked List
- Convert singly linked list into circular linked list
- Split a Circular Linked List into two halves
- Sorted insert for circular linked list
- Circular Singly Linked List | Insertion
- Deletion from a Circular Linked List
- Doubly Circular Linked List | Set 1 (Introduction and Insertion)
- Doubly Circular Linked List | Set 2 (Deletion)
- Circular Linked List | Set 1 (Introduction and Applications)
- Circular Linked List | Set 2 (Traversal)
- Convert a given Binary Tree to Circular Doubly Linked List | Set 2
- Count nodes in Circular linked list
- Exchange first and last nodes in Circular Linked List
- Reverse a circular linked list
- Insertion at Specific Position in a Circular Doubly Linked List
- Convert an Array to a Circular Doubly Linked List
- Reverse a doubly circular linked list
- Search an Element in Doubly Circular Linked List
- Delete every Kth node from circular linked list