VOOZH about

URL: https://www.geeksforgeeks.org/dsa/lucky-alive-person-circle/

⇱ Lucky alive person in a circle | Code Solution to sword puzzle - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lucky alive person in a circle | Code Solution to sword puzzle

Last Updated : 5 Aug, 2022

Given n people standing in a circle where 1st has a sword, find the luckiest person in the circle, if, from 1st soldier who is has a sword each has to kill the next soldier and handover the sword to next soldier, in turn, the soldier will kill the adjacent soldier and handover the sword to next soldier such that one soldier remains in this war who is not killed by anyone.
Prerequisite: Puzzle 81 | 100 people in a circle with a gun puzzle

Examples : 

Input :
Output :
Explanation : 
N = 5 
Soldier 1 2 3 4 5 (5 soldiers) 
In first go 1 3 5 (remains) as 2 and 4 killed by 1 and 3. 
In second go 3 as 5 killed 1 and 3rd kill 5 soldier 3 remains alive.

Input : 100 
Output : 73 
Explanation
N = 10 
Soldiers 1 2 3 4 5 6 7 8 9 10 (10 soldiers) 
In first 1 3 5 7 9 as 2 4 6 8 10 were killed by 1 3 5 7 and 9. 
In second 1 5 9 as 9 kill 1 and in turn 5 kill 9th soldier. 
In third 5 5th soldiers remain alive

Approach: The idea is to use a circular linked list. A circular linked list is made based on a number of soldiers N. As the rule state you have to kill your adjacent soldier and hand over the sword to the next soldier who in turn kills his adjacent soldier and hand over the sword to the next soldier. So in the circular linked list, the adjacent soldier is killed and the remaining soldier fights against each other in a circular way and a single soldier survives who is not killed by anyone. 

Implementation:


Output
73

Time Complexity: O(n), as we are using a loop to traverse n times. Where n is the number of nodes in the linked list.
Auxiliary Space: O(n), as we are using extra space for the linked list. Where n is the number of the nodes in the linked list.

Comment