Lucky alive person in a circle
Submissions: 876   Accuracy:

45.77%

  Difficulty: Medium   Marks: 4

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 remains in this war who is not killed by anyone.

Input:
The first line contains an integer T, number of test cases. For each test case, there is an integer n.

Output:
For each test case, the output is the integer displaying the last person who is alive.

Constraints:
1<=T<=100
1<=n<=10^8

Example:
Input

2
5
10
Output
3
5

Explanation:
1.
In first go 1 3 5 (remains) as 2 and 4 killed by 1 and 3. In second go 3 will remain as 5 killed 1 and 3rd killed 5 hence, 3 remains alive.
2. In first 1 3 5 7 9 remains as 2 4 6 8 10 were killed by 1 3 5 7 and 9. In second 1 5 9 are left as 1 kills 3 and  5 kill the 7th soldier.In third 5th soldiers remain alive as 9 kills the 1st soldier and 5 kill the 9th soldier.

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Vanshika
Author: Vanshika_pec


Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.