Lucky alive person in a circle | Set – 2
Last Updated :
28 Dec, 2023
Given that N person (numbered 1 to N) standing to form a circle. They all have the gun in their hand which is pointed to their leftmost Partner.
Everyone shoots such that 1 shoots 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 the 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: 3
As N = 3 then 1 will shoot 2, 3 will shoot 1 hence 3 is the luckiest person.
Input: N = 8
Output: 1
Here as N = 8, 1 will shoot 2, 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:
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
int stringToNum(string s)
{
stringstream geek(s);
int x = 0;
geek >> x;
return x;
}
int binaryToDecimal(string n)
{
int num = stringToNum(n);
int dec_value = 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 ;
if (num == 0) {
str[i++] = '0' ;
return str;
}
if (num < 0 && base == 10) {
isNegative = true ;
num = -num;
}
while (num != 0) {
int rem = num % base;
str[i++] = (rem > 9) ? (rem - 10) + 'a' : rem + '0' ;
num = num / base;
}
if (isNegative)
str[i++] = '-' ;
reverse(str.begin(), str.end());
return str;
}
char flip( char c)
{
return (c == '0' ) ? '1' : '0' ;
}
string onesComplement(string bin)
{
int n = bin.length(), i;
string ones = "" ;
for (i = 0; i < n; i++)
ones += flip(bin[i]);
return ones;
}
int main()
{
int N = 3;
string arr = "" ;
string ans(itoa(N, arr, 2));
int N_dash = binaryToDecimal(onesComplement(ans));
int luckiest_person = N - N_dash;
cout << luckiest_person;
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int stringToNum(String s)
{
int x = Integer.parseInt(s);
return x;
}
public static int binaryToDecimal(String n)
{
int num = stringToNum(n);
int dec_value = 0 ;
int base = 1 ;
int temp = num;
while (temp != 0 ) {
int last_digit = temp % 10 ;
temp = temp / 10 ;
dec_value += last_digit * base;
base = base * 2 ;
}
return dec_value;
}
public static String itoa( int num, String str, int base)
{
int i = 0 ;
boolean isNegative = false ;
if (num == 0 ) {
str += '0' ;
return str;
}
if (num < 0 && base == 10 ) {
isNegative = true ;
num = -num;
}
while (num != 0 ) {
int rem = num % base;
str += (rem > 9 ) ? ( char )(rem - 10 + 'a' )
: ( char )(rem + '0' );
num = num / base;
}
if (isNegative)
str += '-' ;
StringBuilder sb = new StringBuilder(str);
sb.reverse();
return sb.toString();
}
public static char flip( char c)
{
return (c == '0' ) ? '1' : '0' ;
}
public static String onesComplement(String bin)
{
int n = bin.length(), i;
String ones = "" ;
for (i = 0 ; i < n; i++)
ones += flip(bin.charAt(i));
return ones;
}
public static void main(String[] args)
{
int N = 3 ;
String arr = "" ;
String ans = itoa(N, arr, 2 );
int N_dash = binaryToDecimal(onesComplement(ans));
int luckiest_person = N - N_dash;
System.out.println(luckiest_person);
}
}
|
Python3
def string_to_num(s):
return int (s)
def binary_to_decimal(n):
num = string_to_num(n)
dec_value = 0
base = 1
temp = num
while temp:
last_digit = temp % 10
temp = temp / / 10
dec_value + = last_digit * base
base = base * 2
return dec_value
def itoa(num, base):
i = 0
is_negative = False
str_list = []
if num = = 0 :
str_list.append( '0' )
return ''.join(str_list)
if num < 0 and base = = 10 :
is_negative = True
num = - num
while num ! = 0 :
rem = num % base
str_list.append( str (rem) if rem < = 9 else chr ( ord ( 'A' ) + rem - 10 ))
num / / = base
if is_negative:
str_list.append( '-' )
str_list.reverse()
return ''.join(str_list)
def flip(c):
return '1' if c = = '0' else '0'
def ones_complement( bin ):
n = len ( bin )
ones = ""
for i in range (n):
ones + = flip( bin [i])
return ones
N = 3
ans = itoa(N, 2 )
N_dash = binary_to_decimal(ones_complement(ans))
luckiest_person = N - N_dash
print (luckiest_person)
|
C#
using System;
class MainClass {
public static int stringToNum( string s)
{
int x = int .Parse(s);
return x;
}
public static int binaryToDecimal( string n)
{
int num = stringToNum(n);
int dec_value = 0;
int base_value = 1;
int temp = num;
while (temp != 0) {
int last_digit = temp % 10;
temp = temp / 10;
dec_value += last_digit * base_value;
base_value = base_value * 2;
}
return dec_value;
}
public static string itoa( int num, string str, int base_value)
{
int i = 0;
bool isNegative = false ;
if (num == 0) {
str += '0' ;
return str;
}
if (num < 0 && base_value == 10) {
isNegative = true ;
num = -num;
}
while (num != 0) {
int rem = num % base_value;
str += (rem > 9) ? ( char )(rem - 10 + 'a' )
: ( char )(rem + '0' );
num = num / base_value;
}
if (isNegative)
str += '-' ;
char [] charArray = str.ToCharArray();
Array.Reverse(charArray);
return new string (charArray);
}
public static char flip( char c)
{
return (c == '0' ) ? '1' : '0' ;
}
public static string onesComplement( string bin)
{
int n = bin.Length, i;
string ones = "" ;
for (i = 0; i < n; i++)
ones += flip(bin[i]);
return ones;
}
public static void Main()
{
int N = 3;
string arr = "" ;
string ans = itoa(N, arr, 2);
int N_dash = binaryToDecimal(onesComplement(ans));
int luckiest_person = N - N_dash;
Console.WriteLine(luckiest_person);
}
}
|
Javascript
function stringToNum(s)
{
return parseInt(s);
}
function binaryToDecimal(n)
{
let num = stringToNum(n);
let dec_value = 0;
let base = 1;
let temp = num;
while (temp) {
let last_digit = temp % 10;
temp = Math.floor(temp / 10);
dec_value += last_digit * base;
base = base * 2;
}
return dec_value;
}
function itoa(num, str, base)
{
let i = 0;
let isNegative = false ;
str = str.split( "" );
if (num == 0) {
str[i++] = '0' ;
return str.join( "" );
}
if (num < 0 && base == 10) {
isNegative = true ;
num = -num;
}
while (num != 0) {
let rem = num % base;
str[i++] = (rem > 9) ? (rem - 10): rem;
num = Math.floor(num / base);
}
if (isNegative)
str[i++] = '-' ;
str.reverse();
return str.join( "" );
}
function flip(c)
{
return (c == '0' ) ? '1' : '0' ;
}
function onesComplement(bin)
{
let n = bin.length;
let i;
let ones = "" ;
for (i = 0; i < n; i++)
ones += flip(bin[i]);
return ones;
}
let N = 3;
let arr = "" ;
let ans = itoa(N, arr, 2);
let N_dash = binaryToDecimal(onesComplement(ans));
let luckiest_person = N - N_dash;
console.log(luckiest_person);
|
Alternate Shorter Implementation :
The approach used here is same.
C++
#include <bits/stdc++.h>
using namespace std;
int luckiest_person( int n)
{
int len = log2(n) + 1;
int n2 = n;
for ( int i = 0; i < len; i++) {
n2 = n2 ^ (1 << i);
}
return abs (n - n2);
}
int main()
{
int N = 3;
int lucky_p = luckiest_person(N);
cout << lucky_p;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int luckiest_person( int n)
{
int len = ( int )(Math.log(n) / Math.log( 2 )) + 1 ;
int n2 = n;
for ( int i = 0 ; i < len; i++) {
n2 = n2 ^ ( 1 << i);
}
return Math.abs(n - n2);
}
public static void main(String[] args)
{
int N = 3 ;
int lucky_p = luckiest_person(N);
System.out.println(lucky_p);
}
}
|
Python3
import math
def luckiest_person(n):
len_ = int (math.log(n, 2 )) + 1
n2 = n
for i in range (len_):
n2 = n2 ^ ( 1 << i)
return abs (n - n2)
N = 3
lucky_p = luckiest_person(N)
print (lucky_p)
|
C#
using System;
class GFG {
static int luckiest_person( int n)
{
int len = ( int )(Math.Log(n) / Math.Log(2)) + 1;
int n2 = n;
for ( int i = 0; i < len; i++) {
n2 = n2 ^ (1 << i);
}
return Math.Abs(n - n2);
}
public static void Main()
{
int N = 3;
int lucky_p = luckiest_person(N);
Console.Write(lucky_p);
}
}
|
Javascript
<script>
function luckiest_person(n)
{
let len = parseInt(Math.log(n) / Math.log(2)) + 1;
let n2 = n;
for (let i = 0; i < len; i++) {
n2 = n2 ^ (1 << i);
}
return Math.abs(n - n2);
}
let N = 3;
let lucky_p = luckiest_person(N);
document.write(lucky_p);
</script>
|
Alternate Implementation in O(1) : The approach used here is same, but the operations used are of constant time.
C++
#include <bits/stdc++.h>
using namespace std;
int setBitNumber( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
int luckiest_person( int n)
{
int nearestPower = setBitNumber(n);
return 2 * (n - nearestPower) + 1;
}
int main()
{
int N = 8;
int lucky_p = luckiest_person(N);
cout << lucky_p;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int setBitNumber( int n)
{
n |= n >> 1 ;
n |= n >> 2 ;
n |= n >> 4 ;
n |= n >> 8 ;
n |= n >> 16 ;
n = n + 1 ;
return (n >> 1 );
}
static int luckiest_person( int n)
{
int nearestPower = setBitNumber(n);
return 2 * (n - nearestPower) + 1 ;
}
public static void main(String[] args)
{
int N = 8 ;
int lucky_p = luckiest_person(N);
System.out.print(lucky_p);
}
}
|
Python3
def set_bit_number(n):
n | = n >> 1
n | = n >> 2
n | = n >> 4
n | = n >> 8
n | = n >> 16
n = n + 1
return (n >> 1 )
def luckiest_person(n):
nearest_power = set_bit_number(n)
return 2 * (n - nearest_power) + 1
if __name__ = = "__main__" :
N = 8
lucky_p = luckiest_person(N)
print (lucky_p)
|
C#
using System;
class GFG {
static int setBitNumber( int n)
{
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
static int luckiest_person( int n)
{
int nearestPower = setBitNumber(n);
return 2 * (n - nearestPower) + 1;
}
public static void Main()
{
int N = 8;
int lucky_p = luckiest_person(N);
Console.Write(lucky_p);
}
}
|
Javascript
<script>
function setBitNumber(n) {
n |= n >> 1;
n |= n >> 2;
n |= n >> 4;
n |= n >> 8;
n |= n >> 16;
n = n + 1;
return (n >> 1);
}
function luckiest_person(n) {
let nearestPower = setBitNumber(n);
return 2 * (n - nearestPower) + 1;
}
let N = 8;
let lucky_p = luckiest_person(N);
document.write(lucky_p);
</script>
|
Another approach in O(1) : On the basis of the pattern that forms in given question, which is displayed in following table.
n |
|
1 |
|
2 |
3 |
|
4 |
5 |
6 |
7 |
|
8 |
9 |
10 |
11 |
12 |
13 |
14 |
15 |
|
16 |
|
|
1 |
|
1 |
3 |
|
1 |
3 |
5 |
7 |
|
1 |
3 |
5 |
7 |
9 |
11 |
13 |
15 |
|
1 |
C++
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
int find( int n)
{
int twospower = pow (2, ( int )log2(n));
int diff = n - twospower + 1;
int diffthodd = (2 * diff) - 1;
return diffthodd;
}
int main()
{
int n = 5;
cout << find(n);
return 0;
}
|
Java
import java.util.*;
public class GFG {
public static void main(String[] args) {
int n = 5 ;
System.out.println(find(n));
}
public static int find( int n) {
int twospower = ( int ) Math.pow( 2 , ( int ) (Math.log(n) / Math.log( 2 )));
int diff = n - twospower + 1 ;
int diffthodd = ( 2 * diff) - 1 ;
return diffthodd;
}
}
|
Python3
import math
def find(n):
twospower = int (math. pow ( 2 , int (math.log2(n))))
diff = n - twospower + 1
diffthodd = ( 2 * diff) - 1
return diffthodd
n = 5
print (find(n))
|
C#
using System;
public class GFG {
static public void Main()
{
int n = 5;
Console.WriteLine(find(n));
}
public static int find( int n)
{
int twospower = ( int )Math.Pow(
2, ( int )(Math.Log(n) / Math.Log(2)));
int diff = n - twospower + 1;
int diffthodd = (2 * diff) - 1;
return diffthodd;
}
}
|
Javascript
function find(n) {
let twospower = Math.pow(2, Math.floor(Math.log(n) / Math.log(2)));
let diff = n - twospower + 1;
let diffthodd = 2 * diff - 1;
return diffthodd;
}
let n = 5;
console.log(find(n));
|
Share your thoughts in the comments
Please Login to comment...