Skip to content
Programmingoneonone
Programmingoneonone

Learn everything about programming

  • Home
  • CS Subjects
    • Internet of Things (IoT)
    • 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
Programmingoneonone
Programmingoneonone

Learn everything about programming

HackerEarth Shubham and Xor problem solution

YASH PAL, 31 July 2024
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("%lldn", ans);
return 0;
}
coding problems solutions

Post navigation

Previous post
Next post

Pages

  • About US
  • Contact US
  • Privacy Policy

Follow US

  • YouTube
  • LinkedIn
  • Facebook
  • Pinterest
  • Instagram
©2025 Programmingoneonone | WordPress Theme by SuperbThemes