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
    • 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
Programming101
Programming101

Learn everything about programming

HackerEarth A bitwise pair problem solution

YASH PAL, 31 July 2024
In this HackerEarth A bitwise pair problem solution You are given an array A of N positive integers. Find the number of pairs (i,j) such that:
(A[i] | A[j]) – (A[i] & A[j]) = (A[i] – A[j]), where | is a bitwise OR operator and & is a bitwise AND operator.
HackerEarth A bitwise pair problem solution

HackerEarth A bitwise pair problem solution.

#include<bits/stdc++.h>
#define int long long int
using namespace std;

void solve(){
int n;
cin >> n;
assert(1 <= n and n <= 100000);

int cnt[1001] = {0};
for(int i = 1 ; i <= n ; i++){
int val;
cin >> val;
assert(1 <= val and val <= 1000);
cnt[val]++;
}

int answer = 0;
for(int i = 1 ; i <= 1000 ; i++)
{
for(int j = 1 ; j <= i ; j++)
{
if(i == j)
{
answer += (cnt[i]*cnt[i]);
}
else if((i^j) == (i - j))
{
answer += (cnt[i]*cnt[j]);
}
}
}

cout << answer << endl;
}

signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin >> t;
assert(1 <= t and t <= 10);
while(t--){
solve();
}
}

Second solution

MAXN = 1001
t = int(input())
while t > 0:
t -= 1
n = int(input())
a = list(map(int, input().split()))
cnt = [0] * MAXN
for x in a:
cnt[x] += 1
ans = 0
for i in range(0, MAXN):
for j in range(0, MAXN):
ans += (i ^ j == i - j) * cnt[i] * cnt[j]
print(ans)
coding problems

Post navigation

Previous post
Next post
  • HackerRank Separate the Numbers solution
  • 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
©2025 Programming101 | WordPress Theme by SuperbThemes