C++ Program for Number of unique triplets whose XOR is zero
Last Updated :
27 Aug, 2022
Given N numbers with no duplicates, count the number of unique triplets (ai, aj, ak) such that their XOR is 0. A triplet is said to be unique if all of the three numbers in the triplet are unique.
Examples:
Input : a[] = {1, 3, 5, 10, 14, 15};
Output : 2
Explanation : {1, 14, 15} and {5, 10, 15} are the
unique triplets whose XOR is 0.
{1, 14, 15} and all other combinations of
1, 14, 15 are considered as 1 only.
Input : a[] = {4, 7, 5, 8, 3, 9};
Output : 1
Explanation : {4, 7, 3} is the only triplet whose XOR is 0
Naive Approach: A naive approach is to run three nested loops, the first runs from 0 to n, the second from i+1 to n, and the last one from j+1 to n to get the unique triplets. Calculate the XOR of ai, aj, ak, check if it equals 0. If so, then increase the count.
Time Complexity: O(n3)
Efficient Approach: An efficient approach is to use one of the properties of XOR: the XOR of two of the same numbers gives 0. So we need to calculate the XOR of unique pairs only, and if the calculated XOR is one of the array elements, then we get the triplet whose XOR is 0. Given below are the steps for counting the number of unique triplets:
Below is the complete algorithm for this approach:
- With the map, mark all the array elements.
- Run two nested loops, one from i-n-1, and the other from i+1-n to get all the pairs.
- Obtain the XOR of the pair.
- Check if the XOR is an array element and not one of ai or aj.
- Increase the count if the condition holds.
- Return count/3 as we only want unique triplets. Since i-n and j+1-n give us unique pairs but not triplets, we do a count/3 to remove the other two possible combinations.
Below is the implementation of the above idea:
C++
// CPP program to count the number of
// unique triplets whose XOR is 0
#include <bits/stdc++.h>
using namespace std;
// function to count the number of
// unique triplets whose xor is 0
int countTriplets(int a[], int n)
{
// To store values that are present
unordered_set<int> s;
for (int i = 0; i < n; i++)
s.insert(a[i]);
// stores the count of unique triplets
int count = 0;
// traverse for all i, j pairs such that j>i
for (int i = 0; i < n-1; i++) {
for (int j = i + 1; j < n; j++) {
// xor of a[i] and a[j]
int xr = a[i] ^ a[j];
// if xr of two numbers is present,
// then increase the count
if (s.find(xr) != s.end() && xr != a[i] &&
xr != a[j])
count++;
}
}
// returns answer
return count / 3;
}
// Driver code to test above function
int main()
{
int a[] = {1, 3, 5, 10, 14, 15};
int n = sizeof(a) / sizeof(a[0]);
cout << countTriplets(a, n);
return 0;
}
Output:
2
Time Complexity: O(n2)
Auxiliary Space: O(n) for unordered_set
Please refer complete article on Number of unique triplets whose XOR is zero for more details!
Similar Reads
Number of triplets in array having equal subarray XOR Given an array of integers arr. The task is to count the number of triplets (i, j, k) such that Ai ^ Ai+1 ^ Ai+2 ^ .... ^ Aj-1 = Aj ^ Aj+1 ^ Aj+2 ^ ..... ^ Ak, and 0<=i< j<= k < n, where n is the size of the array.Examples: Input: arr = {5, 2, 7} Output: 2Explanation: The triplets are (0
15 min read
Quadruplet pair with XOR zero in the given Array Given an array arr[] of N integers such that any two adjacent elements in the array differ at only one position in their binary representation. The task is to find whether there exists a quadruple (arr[i], arr[j], arr[k], arr[l]) such that arr[i] ^ arr[j] ^ arr[k] ^ arr[l] = 0. Here ^ denotes the bi
10 min read
Equal Sum and XOR of three Numbers Given an integer N. The task is to count the numbers of pairs of integers A and B such that A + B + N = A ^ B ^ N and A and B are less than N.Examples: Input: N = 2 Output: 3 Explanation:- For N = 2 2 XOR 0 XOR 0 = 2+0+0 2 XOR 0 XOR 1 = 2+0+1 2 XOR 0 XOR 2 != 2+0+2 2 XOR 1 XOR 0 = 2+1+0 2 XOR 1 XOR
5 min read
C++ Program to Count triplets with sum smaller than a given value Given an array of distinct integers and a sum value. Find count of triplets with sum smaller than given sum value. The expected Time Complexity is O(n2).Examples: Input : arr[] = {-2, 0, 1, 3} sum = 2. Output : 2 Explanation : Below are triplets with sum less than 2 (-2, 0, 1) and (-2, 0, 3) Input :
4 min read
Number of subarrays such that XOR of one half is equal to the other Given an array of N numbers, the task is to find the number of sub-arrays (size of the sub-array should be an even number) of the given array such that after dividing the sub-array in two equal halves, bitwise XOR of one half of the sub-array will be equal to bitwise XOR of the other half. Examples:
9 min read