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

HackerEarth The Logic game problem solution

YASH PAL, 31 July 202413 February 2026
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 HackerEarth HackerEarth

Post navigation

Previous post
Next post

Leave a Reply

Your email address will not be published. Required fields are marked *

Programmingoneonone

We at Programmingoneonone, also known as Programming101 is a learning hub of programming and other related stuff. We provide free learning tutorials/articles related to programming and other technical stuff to people who are eager to learn about it.

Pages

  • About US
  • Contact US
  • Privacy Policy

Practice

  • Java
  • C++
  • C

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2026 Programmingoneonone | WordPress Theme by SuperbThemes