Find number of pairs in an array such that their XOR is 0

2

Given an array A[ ] of size N. Find the number of pairs (i, j) such that  A_i XOR  A_j = 0, and 1 <= i < j <= N.

Examples :

Input : A[] = {1, 3, 4, 1, 4}
Output : 2
Explanation : Index (0, 3) and (2, 4)

Input : A[] = {2, 2, 2}
Output : 3

First Approach : Sorting

 A_i XOR  A_j = 0 is only satisfied when  A_i = A_j . Therefore, we will first sort the array and then count the frequency of each element. By combinatorics, we can observe that if frequency of some element is count then, it will contribute count*(count-1)/2 to the answer.
 
Below is the implementation of above approach :

C++

// 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
// count
int calculate(int a[], int n)
{
    // Sorting the list using
    // built in function
    sort(a, a + n);

    int count = 1;
    int answer = 0;

    // Traversing through the
    // elements
    for (int i = 1; i < n; i++) {
    
        if (a[i] == a[i - 1]){

            // Counting frequency of each 
            // elements
            count += 1;
            
        } 
        else 
        {
            // Adding the contribution of
            // the frequency to the answer
            answer = answer + (count * (count - 1)) / 2;
            count = 1;
        }
    }

    answer = answer + (count * (count - 1)) / 2;

    return answer;
}

// Driver Code
int main()
{

    int a[] = { 1, 2, 1, 2, 4 };
    int n = sizeof(a) / sizeof(a[0]);

    // Print the count
    cout << calculate(a, n);
    return 0;
}

// This article is contributed by Sahil_Bansall.

Java

// Java program to find number 
// of pairs in an array such
// that their XOR is 0
import java.util.*;

class GFG 
{
    // Function to calculate 
    // the count
    static int calculate(int a[], int n)
    {
        // Sorting the list using
        // built in function
        Arrays.sort(a);
    
        int count = 1;
        int answer = 0;
    
        // Traversing through the
        // elements
        for (int i = 1; i < n; i++) 
        {
        
            if (a[i] == a[i - 1])
            {
                // Counting frequency of each 
                // elements
                count += 1;
                
            } 
            else
            {
                // Adding the contribution of
                // the frequency to the answer
                answer = answer + (count * (count - 1)) / 2;
                count = 1;
            }
        }
    
        answer = answer + (count * (count - 1)) / 2;
    
        return answer;
    }
    
    // Driver Code
    public static void main (String[] args) 
    {
        int a[] = { 1, 2, 1, 2, 4 };
        int n = a.length;
    
        // Print the count
        System.out.println(calculate(a, n));
    }
}

// This code is contributed by Ansu Kumari.

Python3

# Python3 program to find number of pairs
# in an array such that their XOR is 0

# Function to calculate the count
def calculate(a) :

    # Sorting the list using
    # built in function
    a.sort()
    
    count = 1
    answer = 0
    
    # Traversing through the elements
    for i in range(1, len(a)) :

        if a[i] == a[i - 1] :
            
            # Counting frequncy of each elements
            count += 1

        else :

            # Adding the contribution of
            # the frequency to the answer
            answer = answer + count * (count - 1) // 2
            count = 1

    answer = answer + count * (count - 1) // 2
    
    return answer


# Driver Code
if __name__ == '__main__':
    
    a = [1, 2, 1, 2, 4]

    # Print the count
    print(calculate(a))

Output :

2

Time Complexity : O(N Log N)
 
Second Approach : Hashing (Index Mapping)

Solution is handy, if we can count the frequency of each element in the array. Index mapping technique can be used to count the frequency of each element.

Below is the implementation of above approach :

# Python3 program to find number of pairs
# in an array such that their XOR is 0

# Function to calculate the answer
def calculate(a) :
    
    # Finding the maximum of the array
    maximum = max(a)
    
    # Creating frequency array
    # With initial value 0
    frequency = [0 for x in range(maximum + 1)]
    
    # Traversing through the array 
    for i in a :
         
        # Counting frequency
        frequency[i] += 1
    
    answer = 0
    
    # Traversing through the frequency array
    for i in frequency :
        
        # Calculating answer
        answer = answer + i * (i - 1) // 2
    
    return answer

# Driver Code
a = [1, 2, 1, 2, 4]
print(calculate(a))


Output :

2

Time Complexity : O(N)

Note : Index Mapping method can only be used when the numbers in the array are not large. In such cases, sorting method can be used.


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2 Average Difficulty : 2/5.0
Based on 2 vote(s)