Javascript Program for Number of unique triplets whose XOR is zero
Last Updated :
16 Sep, 2024
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, and 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:
JavaScript
// 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
let s = [];
for (i = 0; i < n; i++)
s.push(a[i]);
// stores the count
// of unique triplets
let 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]
let 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
let a = [1, 3, 5, 10, 14, 15];
let n = a.length;
console.log(countTriplets(a, n));
// This code contributed by Rajput-Ji
Complexity Analysis:
- Time Complexity: O(n2)
- Auxiliary Space: O(n)
Please refer complete article on Number of unique triplets whose XOR is zero for more details!
Similar Reads
Number of unique triplets whose XOR is zero 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
11 min read
Javascript Program to Find all triplets with zero sum Given an array of distinct elements. The task is to find triplets in the array whose sum is zero.Examples : Input : arr[] = {0, -1, 2, -3, 1}Output : (0 -1 1), (2 -3 1)Explanation : The triplets with zero sum are0 + -1 + 1 = 0 and 2 + -3 + 1 = 0 Input : arr[] = {1, -2, 1, 0, 5}Output : 1 -2 1Explana
6 min read
Number of unique pairs whose bitwise XOR and OR are same Given an integer N, the task is to find the number of pairs (say {a, b})from 1 to N (both inclusive) that satisfies the condition: a and b are distinct elements. The values of a | b and a ^ b are equal and a( a | b ) = b( a ^ b ) - ( a | b ) also holds true. Examples: Input: N=5Output:1Explanation:
9 min read
3 Sum - Find Unique Triplets with Zero Sum Given an array arr[], the task is to find all unique triplets {arr[i], arr[j], arr[k]} such that their sum is equal to zero.Note:All indices in a triplet should be distinct (i != j, j != k, k != i).Each triplet should be sorted, i.e., arr[i] <= arr[j] <= arr[k].Examples:Input: arr[] = {0, -1,
15+ min read
Count of triplets till N whose product is at most N Given a positive integer N, the task is to find the number of triplets (A, B, C) from the first N Natural Numbers such that A * B * C ⤠N. Examples: Input: N = 2Output: 4Explanation:Following are the triplets that satisfy the given criteria: ( 1, 1, 1 ) => 1 * 1 * 1 = 1 ⤠2.( 1, 1, 2 ) => 1 *
9 min read
Quadruples for Sum and Product of Integers with XOR Given an integer N, the task is to find X and Y where X is the number of quadruples of positive integers (A, B, C, D) such that AB + CD = N, Y is the number of quadruples of positive integers (A, B, C, D) such that (A + B)(C + D) = N. Print X XOR Y. Examples: Input: N = 4Output: 9Explanation: First
9 min read
Count all triplets from given Array whose bitwise XOR is equal to K Given an array arr[] which contains N positive integers and an integer K. The task is to count all triplets whose XOR is equal to the K. i.e arr[ i ] ^ arr[ j ] ^ arr[ k ] = X where 0 ⤠i < j < k < N ( 0 based indexing) Examples: Input: arr[] = { 2, 1, 3, 7, 5, 4}, K = 5Output: 2Explanation
15+ min read
Maximum value of XOR among all triplets of an array Given an array of integers 'arr', the task is to find the maximum XOR value of any triplet pair among all the possible triplet pairs. Note: An array element can be used more than once. Examples: Input: arr[] = {3, 4, 5, 6} Output: 7 The triplet with maximum XOR value is {4, 5, 6}. Input: arr[] = {1,
4 min read
Count number of triplets with product equal to given number Given an array of distinct integers(considering only positive numbers) and a number âmâ, find the number of triplets with product equal to âmâ. Examples: Input : arr[] = { 1, 4, 6, 2, 3, 8} m = 24Output : 3{1, 4, 6} {1, 3, 8} {4, 2, 3} Input : arr[] = { 0, 4, 6, 2, 3, 8} m = 18Output : 0 Asked in :
15+ min read
3 Sum - Find All Triplets with Zero Sum Given an array arr[], the task is to find all possible indices {i, j, k} of triplet {arr[i], arr[j], arr[k]} such that their sum is equal to zero and all indices in a triplet should be distinct (i != j, j != k, k != i). We need to return indices of a triplet in sorted order, i.e., i < j < k.Ex
11 min read