Skip to content
Programmingoneonone
Programmingoneonone
  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
    • 100+ Java Programs
    • 100+ C Programs
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth The Logic game problem solution

YASH PAL, 31 July 2024
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

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("%dn", 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;
}
coding problems solutions

Post navigation

Previous post
Next post

Related website

The Computer Science

Pages

  • About US
  • Contact US
  • Privacy Policy

Programing Practice

  • C Programs
  • java Programs

HackerRank Solutions

  • C
  • C++
  • Java
  • Python
  • Algorithm

Other

  • Leetcode Solutions
  • Interview Preparation

Programming Tutorials

  • DSA
  • C

CS Subjects

  • Digital Communication
  • Human Values
  • Internet Of Things
  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes