Counts Zeros Xor Pairs | Practice | GeeksforGeeks

Given an array A[] of size N. Find the number of pairs (i, j) such that
Ai XOR Aj = 0, and 1 ≤ i < j ≤ N.
Example 1:
â€‹Input : arr[ ] = {1, 3, 4, 1, 4}
Output : 2
Explanation:
Index( 0, 3 ) and (2 , 4 ) are only pairs 
whose xors is zero so count is 2.


â€‹Example 2:
Input : arr[ ] = {2, 2, 2} 
Output :  3


 
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function calculate() that takes an array (arr), sizeOfArray (n), and return the count of Zeros Xor's Pairs. The driver code takes care of the printing.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(1).


Output:
For each test case, output a single integer i.e counts of Zeros Xors Pairs
Constraints
2 ≤ N ≤ 10^5
1 ≤ A[i] ≤ 10^5
 
0
indutammana023 days ago
   cnt=0   for i in range(0,n-1):       for j in range(i+1,n):            if (arr[i]^arr[j]==0):                cnt+=1   return cnt
+1
yashchawla1163 weeks ago
Simple To Understand And Easy To Implement.
 
https://yashboss116.blogspot.com/2022/04/count-zeros-xor-pairs-geeks-for-geeks.html
 
Approach Explained With Complexities Mentioned.
0
dronzerdracel3 weeks ago
C++|O(n log n) solution with O(1) extra space
long long int calculate(int a[], int n)
{
    // Complete the function
    sort(a,a+n);
    long long int count=1,sum=0;
    for(int i=0;i<n-1;i++){
      if(a[i]==a[i+1])
       count++;
      else{
          sum+=(count-1)*count/2;
          count=1;
      }
    }
    sum+=(count-1)*count/2;
    return sum;
}

0
inforajsoni3 weeks ago
// { Driver Code Starts// C++ program to find number // of pairs in an array such// that their XOR is 0#include <bits/stdc++.h>using namespace std;
// Function to calculate the// countlong long int calculate(int a[], int n);
// Driver Codeint main(){
int t;cin>>t;while(t--){  int n;  cin>>n;  int arr[n+1];  for( int i=0;i<n;i++)        cin>>arr[i];    cout << calculate(arr, n)<<endl;       }return 0;}
// } Driver Code Ends
long long int calculate(int a[], int n){   // Complete the function   long long ans=0;   unordered_map<int,int>mpp;   for(int i=0;i<n;i++)   {       mpp[a[i]]++;   }   for(auto it:mpp)   {       int count=it.second;       ans+=(count*(count-1))/2;   }   return ans;} 
0
shalini21sirothiya3 weeks ago
//c++ 
long long int calculate(int a[], int n){   // Complete the function  map<int,int>mp;  for(int i=0;i<n;i++){      mp[a[i]]++;    }  long long count=0;  for(auto i:mp){      if(i.second>1){          count+=(i.second*(i.second-1))/2;      }  }  return count;         //OR///  sort(a,a+n);  long long count=1;long long ans=0;  for(int i=1;i<n;i++){            if(a[i]==a[i-1]){          count++;      }      else{          ans+=(count*(count-1))/2;          count=1;      }  }   ans+=(count*(count-1))/2;  return ans;} 
0
singhkunal01Premium3 weeks ago
This is The Explanation why Most of solutions contains that Formulae for n numbers : (n*(n-1))/2
 
Taking a case of the array contains all same valued elements can give you a clear intuition behind it like - 
Suppose we have a = [2,2,2,2] (for different values elements we have to sort them or we have to use Frequency array ) , then we need to check the next value for the current index i.e., a[i]==a[i+1] if yes then follow the algorithm steps and all but suppose we have pairs of same values like the given example then-
How we calculate the sum ?
Observe deeply that for every i<j  we have to find the pairs which means for every i we have to check n - 1 - i indices so for example -  
a  = [2,2,2,2] , n = 4 , for 2nd index we have to check 4-1-2 indices = 1 (proved) which makes the formulae which will be added to the result no need  to calculate for all same valued pairs
FORMULA = (N * (N-1)) /2 
 
Thank You.
 
 
-1
harsat20303 weeks ago
simple c++ solution 
long long int calculate(int a[], int n){   sort(a, a+n);
   long long ans = 0;      int cur =0;      for(int i = 0; i < n-1; i++) {       for(int j=i+1;j<n;j++)       {           if(a[i]==a[j]){                         cur++;                      }       }   }      return cur;}
0
vikrantv453 weeks ago
unordered_map<int,int>mp;      for(int i=0;i<n;i++)   {       mp[a[i]]++;   }   long long ans=0;   for(auto it:mp)   {       ans+=(it.second)*(it.second-1);   }   return ans/2;
0
ayan9103 weeks ago
long long int calculate(int a[], int n){   // Complete the function   long long int res = 0;   long long int curr = 1;   sort(a, a+n);   for (int i = 1; i < n; i++) {       if(a[i] == a[i-1]) {           curr++;       } else {           res += curr * (curr-1) / 2;           curr = 1;       }   }   if (curr > 1) {       res += curr * (curr-1) / 2;   }      return res;}
0
raunakmishra12433 weeks ago
long long int calculate(int a[], int n)
{
  
  sort(a,a+n);
  int ans=0;
  int d=0;
  for(int i=0;i<n-1;)
  {
      while(a[i]==a[i+1])
      {
          d++;
          i++;
      }
      ans=ans+(d*(d+1))/2;
      d=0;
      i++;
      
      
  }
  
  return ans;
    
    
    
    
    
}

We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.