In this

**HackerEarth The Logic game problem solution**You and your friend are playing a card game. You have an ordered deck of cards that are numbered from 1 to N where card 1 is placed at the top and card N is placed at the bottom.You both perform an operation on the deck until there are at least 2 cards on the deck. You throw away the card on the top and then your friend moves the card that is now on the top of the deck to the bottom.

You are required to determine the number on the final card that is left in the deck.

## HackerEarth The Logic game problem solution.

`#include <iostream>`

#include <cstdio>

using namespace std;

int findLog(int n){

int cnt = 0;

while(n){

cnt++;

n >>= 1;

}

return cnt - 1;

}

int findLastCard(int n){

if((n & (n-1)) == 0)

return n;

int logValue = findLog(n);

int diff = n - (1 << logValue);

return (diff << 1);

}

int main(){

int n,t;

cin>>t;

while(t--){

scanf("%d", &n);

printf("%d\n", findLastCard(n));

}

return 0;

}

### Second solution

`#include<bits/stdc++.h>`

using namespace std;

int main()

{

int t;

cin>>t;

while(t--)

{

int n;

cin>>n;

if((n&(n-1)) == 0)cout<<n<<"\n";

else

{

int a=log2(n),b=1<<a,c=n^b,ans=2*c;

cout<<ans<<"\n";

}

}

return 0;

}

## 0 Comments