Given an integer N representing N persons standing in a circle, the task is to find the last 2 persons remaining when a person kills their next to the immediate neighbor in a clockwise direction.
Examples:
Input: N = 5
Output: 1 4
Explanation:
Initially: 1 2 3 4 5
=> 1 kills 3
Standing: 1 2 4 5
=> 2 kills 5
Standing: 1 2 4
=> 4 kills 2
Final Standing: 1 4
Input: N = 2
Output: 1 2
Naive Approach: A simple approach is to keep a bool array of size N to keep track of whether a person is alive or not.
- Initially, the boolean array will be true for all persons.
- Keep two pointers, one at the current alive person and second to store previous current person.
- Once found a second alive neighbour from the current person, change its boolean value to false.
- Then again current is updated to next alive from previous.
- This process will continue till last two persons survive.
Time Complexity: O(N2)
Auxiliary Space: O(N)
Efficient Approach: An efficient approach is to remove the person, if dead, from the data structure so that it is not traversed again.
- After one complete round only, there will be only N/2 person, at max.
- Then in the next round it will be left with N/4 person and so on until a number of alive people become 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
struct Node {
int val;
struct Node* next;
Node( int _val)
{
val = _val;
next = NULL;
}
};
void getLastTwoPerson( int n)
{
int total = n;
struct Node* head = new Node(1);
struct Node* temp = head;
for ( int i = 2; i <= n; i++) {
temp->next = new Node(i);
temp = temp->next;
}
temp->next = head;
temp = head;
struct Node* del;
while (total != 2) {
del = temp->next->next;
temp->next->next
= temp->next->next->next;
temp = temp->next;
free (del);
total -= 1;
}
cout << temp->val << " "
<< temp->next->val;
}
int main()
{
int n = 2;
getLastTwoPerson(n);
return 0;
}
|
Java
class GFG{
static class Node
{
int val;
Node next;
Node( int _val)
{
val = _val;
next = null ;
}
};
static void getLastTwoPerson( int n)
{
int total = n;
Node head = new Node( 1 );
Node temp = head;
for ( int i = 2 ; i <= n; i++)
{
temp.next = new Node(i);
temp = temp.next;
}
temp.next = head;
temp = head;
Node del;
while (total != 2 )
{
del = temp.next.next;
temp.next.next = temp.next.next.next;
temp = temp.next;
del = null ;
System.gc();
total -= 1 ;
}
System.out.print(temp.val + " " +
temp.next.val);
}
public static void main(String[] args)
{
int n = 2 ;
getLastTwoPerson(n);
}
}
|
Python3
class newNode:
def __init__( self , val):
self .val = val
self . next = None
def getLastTwoPerson(n):
total = n
head = newNode( 1 )
temp = head
for i in range ( 2 , n + 1 , 1 ):
temp. next = newNode(i)
temp = temp. next
temp. next = head
temp = head
de = None
while (total ! = 2 ):
de = temp. next . next
temp. next . next = temp. next . next . next
temp = temp. next
del de
total - = 1
print (temp.val, temp. next .val)
if __name__ = = '__main__' :
n = 2
getLastTwoPerson(n)
|
C#
using System;
class GFG{
class Node
{
public int val;
public Node next;
public Node( int _val)
{
val = _val;
next = null ;
}
};
static void getLastTwoPerson( int n)
{
int total = n;
Node head = new Node(1);
Node temp = head;
for ( int i = 2; i <= n; i++)
{
temp.next = new Node(i);
temp = temp.next;
}
temp.next = head;
temp = head;
Node del;
while (total != 2)
{
del = temp.next.next;
temp.next.next = temp.next.next.next;
temp = temp.next;
del = null ;
total -= 1;
}
Console.Write(temp.val + " " +
temp.next.val);
}
public static void Main(String[] args)
{
int n = 2;
getLastTwoPerson(n);
}
}
|
Javascript
<script>
class Node {
constructor(val)
{
this .val = val;
this .next = null ;
}
};
function getLastTwoPerson(n)
{
var total = n;
var head = new Node(1);
var temp = head;
for ( var i = 2; i <= n; i++) {
temp.next = new Node(i);
temp = temp.next;
}
temp.next = head;
temp = head;
var del;
while (total != 2) {
del = temp.next.next;
temp.next.next
= temp.next.next.next;
temp = temp.next;
total -= 1;
}
document.write( temp.val + " "
+ temp.next.val);
}
var n = 2;
getLastTwoPerson(n);
</script>
|
Time Complexity: O(N*log N)
Auxiliary Space: O(N)
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 :
16 Sep, 2021
Like Article
Save Article