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 Castle Gate problem solution

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