Given n people standing in a circle where 1st is having sword, find the luckiest person in the circle, if from 1st soldier who is having a sword each have 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 remain in this war who is not killed by anyone.
Prerequisite : Puzzle 81 | 100 people in a circle with gun puzzle
Input : 5
Output : 3
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
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 circular linked list. A circular linked list is made based on number of soldier N. As rule state you have to kill your adjacent soldier and handover the sword to the next soldier who in turn kill his adjacent soldier and handover sword to the next soldier. So in circular linked list the adjacent soldier are killed and the remaining soldier fights against each other in a circular way and a single soldier survive who is not killed by anyone.
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.
- Lucky alive person in a circle | Set - 2
- Position of a person diametrically opposite on a circle
- Program to calculate area of inner circle which passes through center of outer circle and touches its circumference
- Equation of circle when three points on the circle are given
- Check if a circle lies inside another circle or not
- Find area of the larger circle when radius of the smaller circle and difference in the area is given
- Puzzle | Maximum pieces that can be cut from a Circle using 6 straight lines
- Josephus problem | Set 1 (A O(n) Solution)
- Find Two Missing Numbers | Set 2 (XOR based solution)
- Reverse alternate K nodes in a Singly Linked List - Iterative Solution
- Count of obtuse angles in a circle with 'k' equidistant points between 2 given points
- Check whether given circle resides in boundary maintained by two other circles
- Josephus Circle using circular linked list
- Check if a line touches or intersects a circle
- Find minimum radius such that atleast k point lie inside the circle
- Program to find Circumference of a Circle
- Program to find smallest difference of angles of two parts of a given circle
- Count ways to divide circle using N non-intersecting chords
- Draw circle in C graphics
- Find the center of the circle using endpoints of diameter
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.
Improved By : andrew1234