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)