Number of unique triplets whose XOR is zero
Last Updated :
10 Nov, 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.
Below is the implementation of the above idea:
C++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int a[], int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
int xorr = a[i] ^ a[j] ^ a[k];
if (xorr == 0)
count++;
}
}
}
return count;
}
int main()
{
int a[] = { 1, 3, 5, 10, 14, 15 };
int n = sizeof (a) / sizeof (a[0]);
cout << countTriplets(a, n);
return 0;
}
|
Java
public class GFG {
static int countTriplets( int a[], int n)
{
int count = 0 ;
for ( int i = 0 ; i < n - 1 ; i++) {
for ( int j = i + 1 ; j < n; j++) {
for ( int k = j + 1 ; k < n; k++) {
int xorr = a[i] ^ a[j] ^ a[k];
if (xorr == 0 )
count++;
}
}
}
return count;
}
public static void main(String[] args)
{
int a[] = { 1 , 3 , 5 , 10 , 14 , 15 };
int n = 6 ;
System.out.println(countTriplets(a, n));
}
}
|
Python3
class GFG :
@staticmethod
def countTriplets( a, n) :
count = 0
i = 0
while (i < n - 1 ) :
j = i + 1
while (j < n) :
k = j + 1
while (k < n) :
xorr = a[i] ^ a[j] ^ a[k]
if (xorr = = 0 ) :
count + = 1
k + = 1
j + = 1
i + = 1
return count
@staticmethod
def main( args) :
a = [ 1 , 3 , 5 , 10 , 14 , 15 ]
n = 6
print (GFG.countTriplets(a, n))
if __name__ = = "__main__" :
GFG.main([])
|
C#
using System;
class GFG {
static int countTriplets( int [] a, int n)
{
int count = 0;
for ( int i = 0; i < n - 1; i++) {
for ( int j = i + 1; j < n; j++) {
for ( int k = j + 1; k < n; k++) {
int xorr = a[i] ^ a[j] ^ a[k];
if (xorr == 0)
count++;
}
}
}
return count;
}
public static void Main( string [] args)
{
int [] a = { 1, 3, 5, 10, 14, 15 };
int n = 6;
Console.Write(countTriplets(a, n));
}
}
|
Javascript
function countTriplets(a, n)
{
let count = 0;
for (let i = 0; i < n - 1; i++) {
for (let j = i + 1; j < n; j++) {
for (let k = j + 1; k < n; k++) {
let xorr = a[i] ^ a[j] ^ a[k];
if (xorr == 0)
count++;
}
}
}
return count;
}
let a = [ 1, 3, 5, 10, 14, 15 ];
let n = 6;
console.log(countTriplets(a, n));
|
Time Complexity: O(n3), as we will be using nested loops for traversing n3 times.
Auxiliary Space: O(1), as we will not use extra space.
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++
#include <bits/stdc++.h>
using namespace std;
int countTriplets( int a[], int n)
{
unordered_set< int > s;
for ( int i = 0; i < n; i++)
s.insert(a[i]);
int count = 0;
for ( int i = 0; i < n-1; i++) {
for ( int j = i + 1; j < n; j++) {
int xr = a[i] ^ a[j];
if (s.find(xr) != s.end() && xr != a[i] &&
xr != a[j])
count++;
}
}
return count / 3;
}
int main()
{
int a[] = {1, 3, 5, 10, 14, 15};
int n = sizeof (a) / sizeof (a[0]);
cout << countTriplets(a, n);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG
{
static int countTriplets( int []a,
int n)
{
ArrayList<Integer> s =
new ArrayList<Integer>();
for ( int i = 0 ; i < n; i++)
s.add(a[i]);
int count = 0 ;
for ( int i = 0 ; i < n; i++)
{
for ( int j = i + 1 ;
j < n; j++)
{
int xr = a[i] ^ a[j];
if (s.contains(xr) &&
xr != a[i] && xr != a[j])
count++;
}
}
return count / 3 ;
}
public static void main(String srgs[])
{
int []a = { 1 , 3 , 5 ,
10 , 14 , 15 };
int n = a.length;
System.out.print(countTriplets(a, n));
}
}
|
Python3
def countTriplets(a, n):
s = set ()
for i in range (n):
s.add(a[i])
count = 0
for i in range (n):
for j in range (i + 1 , n, 1 ):
xr = a[i] ^ a[j]
if (xr in s and xr ! = a[i] and
xr ! = a[j]):
count + = 1 ;
return int (count / 3 )
if __name__ = = '__main__' :
a = [ 1 , 3 , 5 , 10 , 14 , 15 ]
n = len (a)
print (countTriplets(a, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int countTriplets( int []a,
int n)
{
List< int > s = new List< int >();
for ( int i = 0; i < n; i++)
s.Add(a[i]);
int count = 0;
for ( int i = 0; i < n; i++)
{
for ( int j = i + 1;
j < n; j++)
{
int xr = a[i] ^ a[j];
if (s.Exists(item => item == xr) &&
xr != a[i] && xr != a[j])
count++;
}
}
return count / 3;
}
static void Main()
{
int []a = new int []{1, 3, 5,
10, 14, 15};
int n = a.Length;
Console.Write(countTriplets(a, n));
}
}
|
Javascript
<script>
function countTriplets(a , n) {
var s = [];
for (i = 0; i < n; i++)
s.push(a[i]);
var count = 0;
for (i = 0; i < n; i++) {
for (j = i + 1; j < n; j++) {
var xr = a[i] ^ a[j];
if (s.includes(xr) && xr != a[i] && xr != a[j])
count++;
}
}
return count / 3;
}
var a = [ 1, 3, 5, 10, 14, 15 ];
var n = a.length;
document.write(countTriplets(a, n));
</script>
|
Time Complexity: O(N*N), as we are using a loop to traverse N*N times.
Auxiliary Space: O(N), as we are using extra space for set s.
Share your thoughts in the comments
Please Login to comment...