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
  • Work with US
Programmingoneonone
Programmingoneonone

HackerEarth Maximum AND problem solution

YASH PAL, 31 July 202411 February 2026
In this HackerEarth Maximum AND problem solution we have given two numbers A and B. Find the value of pair (P, Q) such that A <= P < Q <= B value of P AND Q is maximum where AND is a binary operator.
HackerEarth Maximum AND problem solution

HackerEarth Maximum AND problem solution.

#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD 1000000007
int main()
{
ios_base::sync_with_stdio(false);
int t;
cin>>t;
assert(t>=1 && t<=1000);
while(t--)
{
ll a,b,ans;
cin>>a>>b;
assert(b>=1 && b<=1000000000000000000LL && a>=1 && a<b);
if(b-a+1<3)
ans=(a&b);
else if(b&1)
ans=(b&(b-1));
else
ans=((b-1)&(b-2));
cout<<ans<<endl;
}
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 *

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

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