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 = 5Output:1 4Explanation:

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 4Input:N = 2Output: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(N^{2})**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++

`// C++ implementation of the approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Node for a Linked List ` `struct` `Node { ` ` ` `int` `val; ` ` ` `struct` `Node* next; ` ` ` ` ` `Node(` `int` `_val) ` ` ` `{ ` ` ` `val = _val; ` ` ` `next = NULL; ` ` ` `} ` `}; ` ` ` `// Function to find the last 2 survivors ` `void` `getLastTwoPerson(` `int` `n) ` `{ ` ` ` `// Total is the count ` ` ` `// of alive people ` ` ` `int` `total = n; ` ` ` `struct` `Node* head = ` `new` `Node(1); ` ` ` `struct` `Node* temp = head; ` ` ` ` ` `// Initiating the list of n people ` ` ` `for` `(` `int` `i = 2; i <= n; i++) { ` ` ` `temp->next = ` `new` `Node(i); ` ` ` `temp = temp->next; ` ` ` `} ` ` ` ` ` `temp->next = head; ` ` ` `temp = head; ` ` ` ` ` `struct` `Node* del; ` ` ` ` ` `// Total != 2 is terminating ` ` ` `// condition because ` ` ` `// at last only two-person ` ` ` `// will remain alive ` ` ` `while` `(total != 2) { ` ` ` ` ` `// Del represent next person ` ` ` `// to be deleted or killed ` ` ` `del = temp->next->next; ` ` ` `temp->next->next ` ` ` `= temp->next->next->next; ` ` ` `temp = temp->next; ` ` ` `free` `(del); ` ` ` `total -= 1; ` ` ` `} ` ` ` ` ` `// Last two person to ` ` ` `// survive (in any order) ` ` ` `cout << temp->val << ` `" "` ` ` `<< temp->next->val; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 2; ` ` ` ` ` `getLastTwoPerson(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of the approach ` `class` `GFG{ ` ` ` `// Node for a Linked List ` `static` `class` `Node ` `{ ` ` ` `int` `val; ` ` ` `Node next; ` ` ` ` ` `Node(` `int` `_val) ` ` ` `{ ` ` ` `val = _val; ` ` ` `next = ` `null` `; ` ` ` `} ` `}; ` ` ` `// Function to find the last 2 survivors ` `static` `void` `getLastTwoPerson(` `int` `n) ` `{ ` ` ` ` ` `// Total is the count ` ` ` `// of alive people ` ` ` `int` `total = n; ` ` ` `Node head = ` `new` `Node(` `1` `); ` ` ` `Node temp = head; ` ` ` ` ` `// Initiating the list of n people ` ` ` `for` `(` `int` `i = ` `2` `; i <= n; i++) ` ` ` `{ ` ` ` `temp.next = ` `new` `Node(i); ` ` ` `temp = temp.next; ` ` ` `} ` ` ` ` ` `temp.next = head; ` ` ` `temp = head; ` ` ` ` ` `Node del; ` ` ` ` ` `// Total != 2 is terminating ` ` ` `// condition because ` ` ` `// at last only two-person ` ` ` `// will remain alive ` ` ` `while` `(total != ` `2` `) ` ` ` `{ ` ` ` ` ` `// Del represent next person ` ` ` `// to be deleted or killed ` ` ` `del = temp.next.next; ` ` ` `temp.next.next = temp.next.next.next; ` ` ` `temp = temp.next; ` ` ` `del = ` `null` `; ` ` ` ` ` `System.gc(); ` ` ` `total -= ` `1` `; ` ` ` `} ` ` ` ` ` `// Last two person to ` ` ` `// survive (in any order) ` ` ` `System.out.print(temp.val + ` `" "` `+ ` ` ` `temp.next.val); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `n = ` `2` `; ` ` ` ` ` `getLastTwoPerson(n); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

**Output:**

1 2

**Time Complexity:** O(N*log N)**Auxiliary Space:** O(N)

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.

## Recommended Posts:

- Find the number of spectators standing in the stadium at time t
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Program to find area of a circle
- Find the other-end coordinates of diameter in a circle
- Find if a point lies inside a Circle
- Find the Diameter or Longest chord of a Circle
- Find the area of largest circle inscribed in ellipse
- N-Queen Problem | Local Search using Hill climbing with random neighbour
- Find the radii of the circles which are lined in a row, and distance between the centers of first and last circle is given
- Area of the circle that has a square and a circle inscribed in it
- Check if a circle lies inside another circle or not
- Equation of circle when three points on the circle are given
- Probability that two persons will meet
- Permutations to arrange N persons around a circular table
- Time taken by two persons to meet on a circular track
- Maximum number of teams that can be formed with given persons
- Minimum time required to complete a work by N persons together
- Angle subtended by the chord to center of the circle when the angle subtended by the another equal chord of a congruent circle is given
- Minimum Enclosing Circle | Set 1
- Angle subtended by an arc at the centre of a circle

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.