![]() |
VOOZH | about |
Given an array of size N. Find the number of pairs (i, j) such that XOR = 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
XOR = 0 is only satisfied when . 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 then, it will contribute to the answer.
Below is the implementation of above approach:
2
Time Complexity : O(N Log N)
Auxiliary Space: O(1), as no extra space is used
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 :
2
Time Complexity: O(N)
Auxiliary Space: 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.