Open In App

Javascript Program for Number of unique triplets whose XOR is zero

Last Updated : 27 Aug, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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:  

  1. With the map, mark all the array elements.
  2. Run two nested loops, one from i-n-1, and the other from i+1-n to get all the pairs.
  3. Obtain the XOR of the pair.
  4. Check if the XOR is an array element and not one of ai or aj.
  5. Increase the count if the condition holds.
  6. 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:  

Javascript




<script>
    // javascript program to count
    // the number of unique
    // triplets whose XOR is 0
 
    // function to count the
    // number of unique triplets
    // whose xor is 0
    function countTriplets(a , n) {
        // To store values
        // that are present
        var s = [];
        for (i = 0; i < n; i++)
            s.push(a[i]);
 
        // stores the count
        // of unique triplets
        var count = 0;
 
        // traverse for all i,
        // j pairs such that j>i
        for (i = 0; i < n; i++) {
            for (j = i + 1; j < n; j++) {
 
                // xor of a[i] and a[j]
                var xr = a[i] ^ a[j];
 
                // if xr of two numbers
                // is present, then
                // increase the count
                if (s.includes(xr) && xr != a[i] && xr != a[j])
                    count++;
            }
        }
 
        // returns answer
        return count / 3;
    }
 
    // Driver code
    var a = [ 1, 3, 5, 10, 14, 15 ];
    var n = a.length;
    document.write(countTriplets(a, n));
 
// This code contributed by Rajput-Ji
</script>


Output: 

2

Time Complexity: O(n2)

Auxiliary Space: O(n) for array s
 

Please refer complete article on Number of unique triplets whose XOR is zero for more details!



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads