Skip to content
Programmingoneonone
Programmingoneonone
  • CS Subjects
    • Internet of Things (IoT)
    • Digital Communication
    • Human Values
    • Cybersecurity
  • 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
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

Are you a student and stuck with your career or worried about real-time things, and don't know how to manage your learning phase? Which profession to choose? and how to learn new things according to your goal, and land a dream job. Then this might help to you.

Hi My name is YASH PAL, founder of this Blog and a Senior Software engineer with 5+ years of Industry experience. I personally helped 40+ students to make a clear goal in their professional lives. Just book a one-on-one personal call with me for 30 minutes for 300 Rupees. Ask all your doubts and questions related to your career to set a clear roadmap for your professional life.

Book session - https://wa.me/qr/JQ2LAS7AASE2M1

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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