Given that N person (numbered 1 to N) standing as to form a circle. They all have the gun in their hand which is pointed to their leftmost Partner.
Every one shoots such that 1 shoot 2, 3 shoots 4, 5 shoots 6 …. (N-1)the shoot N (if N is even otherwise N shoots 1).
Again on the second iteration, they shoot the rest of remains as above mentioned logic (now for n as even, 1 will shoot to 3, 5 will shoot to 7 and so on).
The task is to find which person is the luckiest(didn’t die)?
Input: N = 3
As N = 3 then 1 will shoot 2, 3 will shoot 1 hence 3 is the luckiest person.
Input: N = 8
Here as N = 8, 1 will shoot 1, 3 will shoot 4, 5 will shoot 6, 7 will shoot 8, Again 1 will shoot 3, 5 will shoot 7, Again 1 will shoot 5 and hence 1 is the luckiest person.
This problem has already been discussed in Lucky alive person in a circle | Code Solution to sword puzzle. In this post, a different approach is discussed.
- Take the Binary Equivalent of N.
- Find its 1’s compliment and convert its equal decimal number N`.
- find |N – N`|.
Below is the implementation of the above approach:
- Lucky alive person in a circle | Code Solution to sword puzzle
- Mid-Point Circle Drawing Algorithm
- Draw a circle without floating point arithmetic
- Check whether an array can be made strictly decreasing by modifying at most one element
- Find if a binary matrix exists with given row and column sums
- Construct the Rooted tree by using start and finish time of its DFS traversal
- Rearrange numbers in an array such that no two adjacent numbers are same
- Merge two BSTs with constant extra space
- Largest sub-string where all the characters appear at least K times
- Sum of Bitwise OR of all pairs in a given array
- Maximum subset sum such that no two elements in set have same digit in them
- Longest prefix in a string with highest frequency
- Find if it is possible to make a binary string which contanins given number of "0", "1" , "01" and "10" as sub sequences
- Convert given integer X to the form 2^N - 1
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.