In this HackerEarth Shubham and Xor problem solution You are given an array of n integer numbers a1, a2, .. ,an. Calculate the number of pair of indices (i,j) such that 1 <= i < j <= n and ai xor aj = 0.


HackerEarth Shubham and Xor problem solution


HackerEarth Shubham and Xor problem solution.

#include <iostream>
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define inf 1000000000000
#define mod 1000000007
#define pb push_back
#define mp make_pair
#define all(v) v.begin(),v.end()
#define S second
#define F first
#define boost1 ios::sync_with_stdio(false);
#define boost2 cin.tie(0);
#define mem(a,val) memset(a,val,sizeof a)
#define endl "\n"
#define maxn 1000005

ll arr[maxn];

inline ll c2(ll x)
{
return (x*(x-1))/2;
}
int main()
{
boost1;boost2;
ll i,j,n,x,y,ans=0,ptr;
cin>>n;
for(i=1;i<=n;i++)
cin>>arr[i];
sort(arr+1,arr+n+1);
ptr=1;
for(i=2;i<=n;i++)
{
if(arr[i]==arr[i-1])
continue;
x=(i-1)-ptr+1;
ans+=c2(x);
ptr=i;
}
x=n-ptr+1;
ans+=c2(x);
cout<<ans;
return 0;
}

Second solution

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

int main(){
int i,j,n,a;
long long ans = 0;
unordered_map<int, int> mp;
scanf("%d", &n);
for(i = 0; i < n; i++){
scanf("%d", &a);
mp[a]++;
ans -= (1ll) * (mp[a] - 1) * (mp[a] - 2) / 2;
ans += (1ll) * (mp[a]) * (mp[a] - 1) / 2;
}
printf("%lld\n", ans);
return 0;
}