- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
We are required to find the number of unique triplets in a given array whose XOR is equal to zero. In other words, we need to find all possible combinations of three numbers in the array, where the XOR of the triplet is zero. Let’s understand this with examples.
Example 1 −
Input: arr = [4, 7, 5, 8, 3, 9] Output: 1
Explanation − In this example, there is only one unique triplet with XOR equal to zero.
That is [4, 7, 3].
Example 2 −
Input: arr = [1, 3, 5, 10, 14, 15] Output: 2
Explanation − In this example, there are two unique triplets with XOR equal to zero.
These are [1, 14, 15] and [5, 10, 15].
Alright, as we understand the problem statement, it’s time to shift focus on various approaches to solve this problem and then pick the best-suited one.
Brute Force − The brute force approach involves finding all possible triplets in the array and checking if their XOR is equal to zero. However, this approach is not feasible for large arrays as it has a time complexity of O(n^3).
Sorting − The sorting approach involves sorting the array and then finding the triplets whose XOR is zero using a two-pointer approach. This approach has a time complexity of O(n^2logn) due to sorting, but it can be optimized to O(n^2) using a hash table.
Hashing − The hashing approach involves using a hash table to store the frequency of elements in the array. We can then iterate through all possible pairs of elements and check if their XOR is present in the hash table. If it is present, we add the frequency of that XOR value to our answer. This approach has a time complexity of O(n^2) and a space complexity of O(n).
Overall, the hashing approach is the most efficient approach to solve this problem as it has a time complexity of O(n^2) and requires only O(n) space. This makes it suitable for large arrays where the brute force approach is not feasible. Moreover, the hashing approach doesn't require sorting, which makes it faster than the sorting approach for this problem. Therefore, we will be using the hashing technique to solve this problem in our tutorial.
Now let’s understand the hashing algorithm to implement this problem.
Define a function named countTriplets with two parameters, an array of integers and its size.
Create an empty array s.
Traverse the array and push all the elements into the array s.
Initialize a variable named count to 0.
Traverse the array and for each element at index i, traverse the array for all elements after i, from index j = i + 1 to n-1.
Calculate the XOR of a[i] and a[j] and store it in a variable xr.
Check if xr is present in the array s and if xr is not equal to a[i] and xr is not equal to a[j], then increase the count by 1.
Once both the loops are completed, divide the count by 3 and return the result.
In the driver code, define an array a and its size n.
Call the countTriplets function with an array a and its size n as arguments and store the result in a variable named result.
The program counts the number of unique triplets in an array whose XOR is equal to zero. It does so by using a hashing technique, specifically by creating a set of the input array to quickly check for values that are present, and then iterating over all pairs of values and checking if their XOR is present in the set. If it is, then the count is incremented. The final count is divided by 3 to obtain the number of unique triplets.
Kickstart Your Career
Get certified by completing the courseGet Started