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.
Input: N = 5
Output: 1 4
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:
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.
- Number of ways to arrange 2*N persons on the two sides of a table with X and Y persons on opposite sides
- Find the number of spectators standing in the stadium at time t
- Immediate smallest number after re-arranging the digits of a given number
- 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
- Equation of circle when three points on the circle are given
- Check if a circle lies inside another circle or not
- 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
- Area of the circle that has a square and a circle inscribed in it
- Permutations to arrange N persons around a circular table
- Probability that two persons will meet
- Minimum time required to complete a work by N persons together
- Time taken by two persons to meet on a circular track
- Maximum number of teams that can be formed with given persons
- Find the index which is the last to be reduced to zero after performing a given operation
- Find maximum value of the last element after reducing the array with given operations
- Find the last remaining element after repeated removal of odd and even indexed elements alternately
- Last digit of a number raised to last digit of N factorial
- Count pieces of circle after N cuts
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.