Skip to content
Programming101
Programming101

Learn everything about programming

  • Home
  • CS Subjects
    • IoT – Internet of Things
    • Digital Communication
    • Human Values
  • Programming Tutorials
    • C Programming
    • Data structures and Algorithms
  • HackerRank Solutions
    • HackerRank Algorithms Solutions
    • HackerRank C problems solutions
    • HackerRank C++ problems solutions
    • HackerRank Java problems solutions
    • HackerRank Python problems solutions
Programming101
Programming101

Learn everything about programming

HackerEarth The Castle Gate problem solution

YASH PAL, 31 July 2024
In this HackerEarth The Castle Gate problem solution Gudi, a fun-loving girl from the city of Dun, travels to Azkahar – a strange land beyond the mountains. She arrives at the gates of Castle Grey, owned by Puchi, the lord of Azkahar to claim the treasure that it guards. However, destiny has other plans for her as she has to move through floors, crossing obstacles on her way to reach the treasure.
The gates of the castle are closed. An integer N is engraved on the gates. Writing on the wall says “Tap the gates as many times as there are unordered pairs of distinct integers from 1 to N whose bit-wise XOR does not exceed N”. Help her find the number of the times she has to tap.
HackerEarth The Castle Gate problem solution

HackerEarth The Castle Gate problem solution.

#include<bits/stdc++.h>


using namespace std;

#define rep(i,n) for(i=0;i<n;i++)
#define ll long long
#define elif else if
#define pii pair<int,int>
#define mp make_pair
#define pb push_back


int main()
{
ios_base::sync_with_stdio(0);
int t;
cin>>t;
assert(1<=t && t<=100);
while(t--)
{
int n,i,j,ans=0;
cin>>n;
assert(2<=n && n<=2000);
for(i=1 ; i<=n ; i++)
{
for( j=i+1; j<=n; j++ )
{
if( (i^j) <= n)
{
ans++;
}
}
}
cout<<ans;
if(t>0)
cout<<endl;
}
return 0;
}

Second solution

from itertools import product
print "n".join([(lambda n: str(len([1 for i,j in product(range(n),repeat=2) if i<j and (i+1)^(j+1) <= n])))(int(raw_input())) for _ in xrange(int(raw_input()))])
coding problems

Post navigation

Previous post
Next post
  • How AI Is Revolutionizing Personalized Learning in Schools
  • GTA 5 is the Game of the Year for 2024 and 2025
  • Hackerrank Day 5 loops 30 days of code solution
  • Hackerrank Day 6 Lets Review 30 days of code solution
  • Hackerrank Day 14 scope 30 days of code solution
©2025 Programming101 | WordPress Theme by SuperbThemes