Josephus Circle using circular linked list

3

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.

Examples:

Input : Length of circle : n = 4
        Count to choose next : m = 2
Output : 3

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.

// CPP program to find last man standing
#include<bits/stdc++.h>
using namespace std;

/* structure for a node in circular
   linked list */
struct Node
{
    int data;
    struct Node *next;
};

// To create a new node of circular
// linked list
Node *newNode(int data)
{
   Node *temp = new Node;
   temp->next = temp;
   temp->data = data;
}

/* Function to find the only person left
   after one in every m-th node is killed
   in a circle of n nodes */
void getJosephusPosition(int m, int n)
{
    // Create a circular linked list of
    // size N.
    Node *head = newNode(1);
    Node *prev = head;
    for (int i = 2; i <= n; i++)
    {
        prev->next = newNode(i);
        prev = prev->next;
    }
    prev->next = head; // Connect last
                       // node to first

    /* while only one node is left in the
    linked list*/
    Node *ptr1 = head, *ptr2 = head;
    while (ptr1->next != ptr1)
    {
        // Find m-th node
        int count = 1;
        while (count != m)
        {
            ptr2 = ptr1;
            ptr1 = ptr1->next;
            count++;
        }

        /* Remove the m-th node */
        ptr2->next = ptr1->next;
        ptr1 = ptr2->next;
    }

    printf ("Last person left standing "
            "(Josephus Position) is %d\n ",
            ptr1->data);
}

/* Driver program to test above functions */
int main()
{
    int n = 14, m = 2;
    getJosephusPosition(m, n);
    return 0;
}

Output :

Last person left standing (Josephus Position) is 9

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 contribute@geeksforgeeks.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.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



3 Average Difficulty : 3/5.0
Based on 2 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.