Josephus Circle using circular linked list

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 : 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.

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Code to find the last man Standing 
public class GFG {
      
    // Node class to store data 
    static class Node
    {
        public int data ;
        public Node next;
        public Node( int data )
        {
            this.data = data;
        }
    }
      
    /* Function to find the only person left
    after one in every m-th node is killed
    in a circle of n nodes */
    static void getJosephusPosition(int m, int n)
    {
        // Create a circular linked list of
        // size N.
        Node head = new Node(1);
        Node prev = head;
        for(int i = 2; i <= n; i++)
        {
            prev.next = new Node(i);
            prev = prev.next;
        }
          
        // Connect last node to first
        prev.next = head;
          
        /* 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;
        }
        System.out.println ("Last person left standing " +
                 "(Josephus Position) is " + ptr1.data);
    }
      
    /* Driver program to test above functions */
    public static void main(String args[])
    {
        int n = 14, m = 2;
        getJosephusPosition(m, n);
    }
}

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Code to find the last man Standing 
using System;
public class GFG { 
      
    // Node class to store data 
    class Node 
    
        public int data ; 
        public Node next; 
        public Node( int data ) 
        
            this.data = data; 
        
    
      
    /* Function to find the only person left 
    after one in every m-th node is killed 
    in a circle of n nodes */
    static void getJosephusPosition(int m, int n) 
    
        // Create a circular linked list of 
        // size N. 
        Node head = new Node(1); 
        Node prev = head; 
        for(int i = 2; i <= n; i++) 
        
            prev.next = new Node(i); 
            prev = prev.next; 
        
          
        // Connect last node to first 
        prev.next = head; 
          
        /* 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; 
        
        Console.WriteLine ("Last person left standing "
                "(Josephus Position) is " + ptr1.data); 
    
      
    /* Driver program to test above functions */
     static public void Main(String []args) 
    
        int n = 14, m = 2; 
        getJosephusPosition(m, n); 
    
//contributed by Arnab Kundu

chevron_right


Output :

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 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.



My Personal Notes arrow_drop_up