# Lucky alive person in a circle | Set – 2

• Difficulty Level : Easy
• Last Updated : 15 Jun, 2022

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)?

Examples

Input: N = 3
Output:
As N = 3 then 1 will shoot 2, 3 will shoot 1 hence 3 is the luckiest person.

Input: N = 8
Output:
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.

Approach:

1. Take the Binary Equivalent of N.
2. Find its 1’s compliment and convert its equal decimal number N`.
3. find |N – N`|.

Below is the implementation of the above approach:

## C++

 // C++ implementation of the above approach#include using namespace std; // Function to convert string to numberint stringToNum(string s){     // object from the class stringstream    stringstream geek(s);     // The object has the value 12345 and stream    // it to the integer x    int x = 0;    geek >> x;     return x;} // Function to convert binary to decimalint binaryToDecimal(string n){    int num = stringToNum(n);    int dec_value = 0;     // Initializing base value to 1, i.e 2^0    int base = 1;     int temp = num;    while (temp) {        int last_digit = temp % 10;        temp = temp / 10;         dec_value += last_digit * base;         base = base * 2;    }     return dec_value;} string itoa(int num, string str, int base){    int i = 0;    bool isNegative = false;     /* Handle 0 explicitly, otherwise    empty string is printed for 0 */    if (num == 0) {        str[i++] = '0';        return str;    }     // In standard itoa(), negative numbers    // are handled only with base 10.    // Otherwise numbers are considered unsigned.    if (num < 0 && base == 10) {        isNegative = true;        num = -num;    }     // Process individual digits    while (num != 0) {        int rem = num % base;        str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0';        num = num / base;    }     // If the number is negative, append '-'    if (isNegative)        str[i++] = '-';     // Reverse the string    reverse(str.begin(), str.end());     return str;} char flip(char c){    return (c == '0') ? '1' : '0';} // Function to find the ones complementstring onesComplement(string bin){    int n = bin.length(), i;     string ones = "";     // for ones complement flip every bit    for (i = 0; i < n; i++)        ones += flip(bin[i]);     return ones;} // Driver codeint main(){    // Taking the number of a person    // standing in a circle.    int N = 3;    string arr = "";     // Storing the binary equivalent in a string.    string ans(itoa(N, arr, 2));     // taking one's compelement and    // convert it to decimal value    int N_dash = binaryToDecimal(onesComplement(ans));     int luckiest_person = N - N_dash;     cout << luckiest_person;     return 0;}

Output

3

Alternate Shorter Implementation :
The approach used here is same.

## C++

 // C++ implementation of the above approach#include using namespace std; int luckiest_person(int n){    // to calculate the number of bits in    // the binary equivalent of n    int len = log2(n) + 1;     // Finding complement by inverting the    // bits one by one from last    int n2 = n;    for (int i = 0; i < len; i++) {         // XOR of n2 with (1<

## Java

 // Java implementation of the above approachimport java.io.*; class GFG {     static int luckiest_person(int n)    {         // To calculate the number of bits in        // the binary equivalent of n        int len = (int)(Math.log(n) / Math.log(2)) + 1;         // Finding complement by inverting the        // bits one by one from last        int n2 = n;        for (int i = 0; i < len; i++) {             // XOR of n2 with (1<

## Python3

 # Python3 implementation of the above approachimport math def luckiest_person(n):     # to calculate the number of bits in    # the binary equivalent of n    len_ = int(math.log(n, 2)) + 1     # Finding complement by inverting the    # bits one by one from last    n2 = n    for i in range(len_):         # XOR of n2 with (1<

## C#

 // C# implementation of the above approachusing System; class GFG {     static int luckiest_person(int n)    {         // To calculate the number of bits in        // the binary equivalent of n        int len = (int)(Math.Log(n) / Math.Log(2)) + 1;         // Finding complement by inverting the        // bits one by one from last        int n2 = n;        for (int i = 0; i < len; i++) {             // XOR of n2 with (1<

## Javascript



Output

3

Alternate Implementation in O(1) : The approach used here is same, but the operations used are of constant time.

## C++

 // Here is a O(1) solution for this problem// it will work for 32 bit integers]#include using namespace std; // function to find the highest power of 2// which is less than nint setBitNumber(int n){    // Below steps set bits after    // MSB (including MSB)     // Suppose n is 273 (binary    // is 100010001). It does following    // 100010001 | 010001000 = 110011001    n |= n >> 1;     // This makes sure 4 bits    // (From MSB and including MSB)    // are set. It does following    // 110011001 | 001100110 = 111111111    n |= n >> 2;     n |= n >> 4;    n |= n >> 8;    n |= n >> 16;     // Increment n by 1 so that    // there is only one set bit    // which is just before original    // MSB. n now becomes 1000000000    n = n + 1;     // Return original MSB after shifting.    // n now becomes 100000000    return (n >> 1);} int luckiest_person(int n){    // to calculate the highest number which    // is power of 2 and less than n    int nearestPower = setBitNumber(n);     // return the correct answer as per the    // approach in above article    return 2 * (n - nearestPower) + 1;}int main(){    int N = 8;    int lucky_p = luckiest_person(N);    cout << lucky_p;    return 0;} // This code is contributed by Ujesh Maurya

## Java

 /*package whatever //do not write package name here */ import java.io.*; // Here is a O(1) solution for this problem// it will work for 32 bit integers]class GFG {    static int setBitNumber(int n)    {         // Below steps set bits after        // MSB (including MSB)         // Suppose n is 273 (binary        // is 100010001). It does following        // 100010001 | 010001000 = 110011001        n |= n >> 1;         // This makes sure 4 bits        // (From MSB and including MSB)        // are set. It does following        // 110011001 | 001100110 = 111111111        n |= n >> 2;         n |= n >> 4;        n |= n >> 8;        n |= n >> 16;         // Increment n by 1 so that        // there is only one set bit        // which is just before original        // MSB. n now becomes 1000000000        n = n + 1;         // Return original MSB after shifting.        // n now becomes 100000000        return (n >> 1);    }     static int luckiest_person(int n)    {        // to calculate the highest number which        // is power of 2 and less than n        int nearestPower = setBitNumber(n);         // return the correct answer as per the        // approach in above article        return 2 * (n - nearestPower) + 1;    }       // Driver Code    public static void main(String[] args)    {        int N = 8;        int lucky_p = luckiest_person(N);         System.out.print(lucky_p);    }} // This code is contributed by Ujesh Maurya

## C#

 // Here is a O(1) solution for this problem// it will work for 32 bit integers]using System; class GFG {     // function to find the highest power of 2    // which is less than n    static int setBitNumber(int n)    {        // Below steps set bits after        // MSB (including MSB)         // Suppose n is 273 (binary        // is 100010001). It does following        // 100010001 | 010001000 = 110011001        n |= n >> 1;         // This makes sure 4 bits        // (From MSB and including MSB)        // are set. It does following        // 110011001 | 001100110 = 111111111        n |= n >> 2;         n |= n >> 4;        n |= n >> 8;        n |= n >> 16;         // Increment n by 1 so that        // there is only one set bit        // which is just before original        // MSB. n now becomes 1000000000        n = n + 1;         // Return original MSB after shifting.        // n now becomes 100000000        return (n >> 1);    }     static int luckiest_person(int n)    {         // to calculate the highest number which        // is power of 2 and less than n        int nearestPower = setBitNumber(n);         // return the correct answer as per the        // approach in above article        return 2 * (n - nearestPower) + 1;    }     // Driver code    public static void Main()    {        int N = 8;        int lucky_p = luckiest_person(N);         Console.Write(lucky_p);    }} // This code is contributed by Ujesh Maurya

## Javascript



Output

1

Another approach in O(1) : On the basis of the pattern that forms in given question, which is displayed in following table.

## C++

 #include #include using namespace std; // Driven codeint find(int n){    // Obtain number less n in 2's power    int twospower = pow(2, (int)log2(n));     // Find p-position of odd number, in odd series    int diff = n - twospower + 1;     // Value of pth odd number    int diffthodd = (2 * diff) - 1;     return diffthodd;}// Driver codeint main(){    int n = 5;    cout << find(n);    return 0;} // This code is contributed by Dharmik Parmar

Output

3

My Personal Notes arrow_drop_up