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 person n and a number k which indicates that k-1 persons are skipped and the kth person is killed in the circle. The task is to choose the place in the initial circle so that you are the last one remaining and so survive. (0-based indexing) .
Examples :
Input : Length of circle : n = 4
Count to choose next : k = 2
Output : 0
Input : n = 5
k = 3
Output : 3
We have already discussed different solutions to this problem (here, here, and here). In this post, a C++ STL-based solution using a list container is discussed which uses the idea of a circular list.
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
int josephusCircle( int n, int k){
list< int >l;
for ( int i=0;i<n;i++)
l.push_back(i);
auto it = l.begin();
while (l.size()>1){
for ( int i=1;i<k;i++){
it++;
if (it==l.end()){
it = l.begin();
}
}
it = l.erase(it);
if (it==l.end()){
it = l.begin();
}
}
return l.front();
}
int main()
{
int n=14,k=2;
cout<<josephusCircle(n,k)<< "\n" ;
return 0;
}
|
Java
public class GFG {
static class Node
{
public int data ;
public Node next;
public Node( int data )
{
this .data = data;
}
}
static void getJosephusPosition( int m, int n)
{
Node head = new Node( 0 );
Node prev = head;
for ( int i = 1 ; i < n; i++)
{
prev.next = new Node(i);
prev = prev.next;
}
prev.next = head;
Node ptr1 = head, ptr2 = head;
while (ptr1.next != ptr1)
{
int count = 1 ;
while (count != m)
{
ptr2 = ptr1;
ptr1 = ptr1.next;
count++;
}
ptr2.next = ptr1.next;
ptr1 = ptr2.next;
}
System.out.println ( "Last person left standing " +
"(Josephus Position) is " + ptr1.data);
}
public static void main(String args[])
{
int n = 14 , m = 2 ;
getJosephusPosition(m, n);
}
}
|
Python3
class Node:
def __init__( self , x):
self .data = x
self . next = None
def getJosephusPosition(m, n):
head = Node( 0 )
prev = head
for i in range ( 1 , n):
prev. next = Node(i)
prev = prev. next
prev. next = head
ptr1 = head
ptr2 = head
while (ptr1. next ! = ptr1):
count = 1
while (count ! = m):
ptr2 = ptr1
ptr1 = ptr1. next
count + = 1
ptr2. next = ptr1. next
ptr1 = ptr2. next
print ( "Last person left standing (Josephus Position) is " , ptr1.data)
if __name__ = = '__main__' :
n = 14
m = 2
getJosephusPosition(m, n)
|
C#
using System;
public class GFG {
class Node
{
public int data ;
public Node next;
public Node( int data )
{
this .data = data;
}
}
static void getJosephusPosition( int m, int n)
{
Node head = new Node(0);
Node prev = head;
for ( int i = 1; i < n; i++)
{
prev.next = new Node(i);
prev = prev.next;
}
prev.next = head;
Node ptr1 = head, ptr2 = head;
while (ptr1.next != ptr1)
{
int count = 1;
while (count != m)
{
ptr2 = ptr1;
ptr1 = ptr1.next;
count++;
}
ptr2.next = ptr1.next;
ptr1 = ptr2.next;
}
Console.WriteLine ( "Last person left standing " +
"(Josephus Position) is " + ptr1.data);
}
static public void Main(String []args)
{
int n = 14, m = 2;
getJosephusPosition(m, n);
}
}
|
Javascript
<script>
class Node {
constructor(val) {
this .data = val;
this .next = null ;
}
}
function getJosephusPosition(m , n) {
var head = new Node(0);
var prev = head;
for (i = 1; i < n; i++) {
prev.next = new Node(i);
prev = prev.next;
}
prev.next = head;
var ptr1 = head, ptr2 = head;
while (ptr1.next != ptr1) {
var count = 1;
while (count != m) {
ptr2 = ptr1;
ptr1 = ptr1.next;
count++;
}
ptr2.next = ptr1.next;
ptr1 = ptr2.next;
}
document.write( "Last person left standing " + "(Josephus Position) is " + ptr1.data);
}
var n = 14, m = 2;
getJosephusPosition(m, n);
</script>
|
Time complexity: O(k * n), as we are using nested loops to traverse k*n time.
Auxiliary Space: O(n), as we are using extra space for the linked list.
This article is Improved by Mechanizer. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
23 Feb, 2023
Like Article
Save Article