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 is 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, 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 to 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 that XOR of two same numbers gives 0. So we need to calculate XOR of unique pairs only, and if the calculated XOR is one of the array element, 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 of this approach:
- With map, mark all the array elements.
- Run two nested loops, one from i-n, and the other from i+1-n to get all the pairs.
- Obtain the XOR of 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 gives us unique pairs but not triplets, so we do a count/3 to remove the other two possible combinations.
Below is the implementation of 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; 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; } |
Java
// Java program to count // the number of unique // triplets whose XOR is 0 import java.io.*; import java.util.*; class GFG { // function to count the // number of unique triplets // whose xor is 0 static int countTriplets(int []a, int n) { // To store values // that are present ArrayList<Integer> s = new ArrayList<Integer>(); for (int i = 0; i < n; i++) s.add(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; 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.contains(xr) && xr != a[i] && xr != a[j]) count++; } } // returns answer return count / 3; } // Driver code public static void main(String srgs[]) { int []a = {1, 3, 5, 10, 14, 15}; int n = a.length; System.out.print(countTriplets(a, n)); } } // This code is contributed by // Manish Shaw(manishshaw1) |
Python3
# Python 3 program to count the number of # unique triplets whose XOR is 0 # function to count the number of # unique triplets whose xor is 0 def countTriplets(a, n): # To store values that are present s = set() for i in range(n): s.add(a[i]) # stores the count of unique triplets count = 0 # traverse for all i, j pairs such that j>i for i in range(n): for j in range(i + 1, n, 1): # xor of a[i] and a[j] xr = a[i] ^ a[j] # if xr of two numbers is present, # then increase the count if (xr in s and xr != a[i] and xr != a[j]): count += 1; # returns answer return int(count / 3) # Driver code if __name__ == '__main__': a = [1, 3, 5, 10, 14, 15] n = len(a) print(countTriplets(a, n)) # This code is contributed by # Surendra_Gangwar |
C#
// C# program to count // the number of unique // triplets whose XOR is 0 using System; using System.Collections.Generic; class GFG { // function to count the // number of unique triplets // whose xor is 0 static int countTriplets(int []a, int n) { // To store values // that are present List<int> s = new List<int>(); for (int i = 0; i < n; i++) s.Add(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; 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.Exists(item => item == xr) && xr != a[i] && xr != a[j]) count++; } } // returns answer return count / 3; } // Driver code static void Main() { int []a = new int[]{1, 3, 5, 10, 14, 15}; int n = a.Length; Console.Write(countTriplets(a, n)); } } // This code is contributed by // Manish Shaw(manishshaw1) |
Output:
2
Time Complexity : O(n2)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum number of unique Triplets such that each element is selected only once
- All unique triplets that sum up to a given value
- Count number of triplets with product equal to given number with duplicates allowed
- Count number of triplets with product equal to given number
- Count number of triplets with product equal to given number | Set 2
- Find number of triplets in array such that a[i]>a[j]>a[k] and i<j<k
- Count triplets such that sum of any two number is equal to third | Set 2
- Number of distinct ways to represent a number as sum of K unique primes
- Total number of triplets (A, B, C) in which the points B and C are Equidistant to A
- Number of triplets in array having subarray xor equal
- Count the number of unordered triplets with elements in increasing order and product less than or equal to integer X
- Number of unique pairs in an array
- String with maximum number of unique characters
- Count number of Unique Triangles using Operator overloading
- Maximum number of unique values in the array after performing given operations
- Divide the array into minimum number of sub-arrays having unique elements
- Find number of unique triangles among given N triangles
- Print all triplets with given sum
- Find all triplets with zero sum
- Print triplets with sum less than k
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

