Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Problem Statement
3.
Example
3.1.
Input
3.2.
Output
4.
Brute Force Approach
5.
Algorithm using the hash table
5.1.
C++ Code
5.2.
Java Code
5.3.
Input
5.4.
Output
6.
Complexities
6.1.
Time complexity 
6.2.
Space complexity
7.
Frequently Asked Questions
7.1.
What do you mean by HashTable?
7.2.
What are the hash table's three fundamental operations?
7.3.
What is the time complexity of a hash table?
7.4.
What is the load factor of a hash table?
7.5.
How come hash tables are quick?
8.
Conclusion
Last Updated: Mar 27, 2024
Easy

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

Author Manan Singhal
0 upvote

Introduction

In the article, we are going to discuss one of the popular questions which  to find the number of pairs in an array such that their XOR is 0.

Problem Statement

Suppose you have been given an array of N integers. You need to find the number of pairs(i, j) that will satisfy the below condition:

arr[i] XOR arr[j] = 0 and 1<=i<j<=n

This states that you need to find the number of pairs present in the array that satisfy the above condition. With the below example, the explanation you are going to understand the problem statement much better.

Example

Input

arr[] = {1, 3, 4, 1, 4}

Output

2


Explanation: (0, 3) contains element 1 at both position, so XOR of same element will going to be 0, and similarly, (2, 4) contains element 4 at both position, so XOR of both will going to be 0.

Brute Force Approach

  1. Firstly, we will sort the given array and then count the frequency of each element.
  2. Combinatorics allows us to see that if an element's frequency is count, it will add count*(count-1)/2 to the solution.

Algorithm using the hash table

  1. We need to calculate the frequency of each element in the input array. The frequency of each element can be counted using the index mapping technique.
  2. Combinatorics allows us to see that if an element's frequency is count, it will add count*(count-1)/2 to the solution.

C++ Code

/* 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 count the required condition */
int calculate(int a[], int n){

	/* retrieving maximum element of the array */
	int *maximum = max_element(a, a + n);
	int frequency[*maximum + 1] = {0};

	/* Traversing through the list */
	for(int i = 0; i < n; i++)
	{
		/* Counting how often each element occurs */
		frequency[a[i]] += 1;
	}
	int answer = 0;

	/* Traversing through the frequency list */
	for(int i = 0; i < (*maximum)+1; i++)
	{
		answer = answer + frequency[i] * (frequency[i] - 1) ;
	}

	return answer/2;
}

/* Main program */
int main()
{
    /* Input array */
    int arr[] = { 1, 3, 4, 1, 4 };
    
    /* Size of an input array */
    int n = sizeof(arr) / sizeof(arr[0]);
    
    /* Printing required output */
    cout << calculate(arr, n);
    
    return 0;
}
You can also try this code with Online C++ Compiler
Run Code

Java Code

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

class Main {

	/* function to count the required condition */
	static int calculate(int a[], int n)
	{
		/* retrieving maximum element of the array */
		int maximum = Arrays.stream(a).max().getAsInt();
		int frequency[] = new int[maximum + 1];

		/* Traversing through the list */
		for (int i = 0; i < n; i++)
		{
			/* Counting how often each element occurs */
			frequency[a[i]] += 1;
		}
		int answer = 0;

		/* Traversing through the frequency list */
		for (int i = 0; i < (maximum) + 1; i++)
		{
			answer = answer + frequency[i] * (frequency[i] - 1);
		}
		return answer / 2;
	}
	
	/* Main program */
	public static void main(String[] args)
	{
    	/* Input array */
        int arr[] = { 1, 3, 4, 1, 4 };
        
        /* Size of an input array */
        int n = arr.length;
        
        /* Printing required output */
        System.out.println(calculate(arr, n));
	}
}
You can also try this code with Online Java Compiler
Run Code

Input

arr[] = {1, 3, 4, 1, 4}

Output

2

Complexities

Time complexity 

O(n), where n is the size of an array
Reason: This method requires traversing the array once so that the time complexity will be O(n).

Space complexity

O(n), where n is the size of an array
Reason: Required an O(n) space to store the frequency of each element of an array.

You can also read about the Longest Consecutive Sequence.

Frequently Asked Questions

What do you mean by HashTable?

A hash table associates various keys with various values. Hash table implements the map, the serializable, and cloneable interfaces and inherits the dictionary type.

What are the hash table's three fundamental operations?

The fundamental principle operations of a hash table are: search which searches for an element in the hash table, Insert which adds a new element to the hash table, and Delete which removes an element from the hash table.

What is the time complexity of a hash table?

No matter how many entries are in the table, hash tables typically offer constant-time O(1) lookup. The majority of hash table schemes have an O(n) worst-case lookup time.

What is the load factor of a hash table?

Load factor can be defined as (m/n), where m is the preferred number of entries that can be added before the size of the underlying data structure needs to be increased and n is the overall size of the hash table.

How come hash tables are quick?

The linear time complexity of O(n) is required when searching through an array-like data structure that is the search time grows linearly in proportion to the size of the data structure. Simply, searching through an array is slower than using a hash table.

Conclusion

In this article, we have found the number of pairs in an array such that their XOR is 0. We hope that this blog will help you understand the concept of a hash table, and if you want to learn more about the hash table, check out our other blogs on the hashtablehashtable vs stl map, the difference between hashmap and hashtable, and Hash Function in Data 

Recommended problems -

 

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass