3514. Number of Unique XOR Triplets II
Difficulty: Medium
Topics: Staff, Array, Math, Bit Manipulation, Enumeration, Biweekly Contest 154
You are given an integer array nums.
A XOR triplet is defined as the XOR of three elements nums[i] XOR nums[j] XOR nums[k] where i <= j <= k.
Return the number of unique XOR triplet values from all possible triplets (i, j, k).
Example 1:
- Input: nums = [1,3]
- Output: 2
-
Explanation:
- The possible XOR triplet values are:
(0, 0, 0) → 1 XOR 1 XOR 1 = 1(0, 0, 1) → 1 XOR 1 XOR 3 = 3(0, 1, 1) → 1 XOR 3 XOR 3 = 1(1, 1, 1) → 3 XOR 3 XOR 3 = 3
- The unique XOR values are
{1, 3}. Thus, the output is 2.
- The possible XOR triplet values are:
Example 2:
- Input: nums = [6,7,8,9]
- Output: 4
-
Explanation: The possible XOR triplet values are
{6, 7, 8, 9}. Thus, the output is 4.
Example 3:
- Input: nums = [5, 5, 5]
- Output: 1
Example 4:
- Input: nums = [1]
- Output: 1
Example 5:
- Input: nums = [1500, 1500, 1500]
- Output: 1
Example 6:
- Input: nums = [1, 2, 4, 8, 16]
- Output: 15
Constraints:
1 <= nums.length <= 15001 <= nums[i] <= 1500
Hint:
- What is the maximum possible XOR value achievable by any triplet?
- Let the maximum possible XOR value be stored in
max_xor. - For each index
i, consider all pairs of indices(j, k)such thati <= j <= k. For each such pair, compute the triplet XOR asnums[i] XOR nums[j] XOR nums[k]. - You can optimize the calculation by precomputing or reusing intermediate XOR results. For example, after fixing an index
i, compute XORs of pairs(j, k)inO(n²)time instead of checking all three indices independently. - Finally, count the number of unique XOR values obtained from all triplets.
Solution:
We need to count the number of unique XOR values obtainable from all triplets (i, j, k) where i <= j <= k. We solve this by enumerating all valid triplets using a nested loop structure that respects the index ordering constraint, computing each triplet's XOR value, and tracking uniqueness with a boolean array sized to the maximum possible XOR result (2047, since numbers are ≤1500 and XOR of three such numbers fits in 11 bits).
Approach
- Determine the maximum possible XOR value based on input constraints.
- Initialize a boolean array
seento mark which XOR values have been produced. - Use three nested loops with indices
i ≤ j ≤ kto generate all triplets. - Optimize by precomputing the XOR of the first two elements (
nums[i] ^ nums[j]) before entering the third loop. - For each triplet, compute the final XOR and mark it in
seen. - Count and return the number of
trueentries inseen.
Let's implement this solution in PHP: 3514. Number of Unique XOR Triplets II
<?php
/**
* @param Integer[] $nums
* @return Integer
*/
function uniqueXorTriplets(array $nums): int
{
...
...
...
/**
* go to ./solution.php
*/
}
// Test cases
echo uniqueXorTriplets([1, 3]) . "\n"; // Output: 2
echo uniqueXorTriplets([6, 7, 8, 9]) . "\n"; // Output: 4
echo uniqueXorTriplets([5, 5, 5]) . "\n"; // Output: 1
echo uniqueXorTriplets([1]) . "\n"; // Output: 1
echo uniqueXorTriplets([1500, 1500, 1500]) . "\n"; // Output: 1
echo uniqueXorTriplets([1, 2, 4, 8, 16]) . "\n"; // Output: 15
?>
Explanation:
- The triplet definition requires
i <= j <= k, meaning indices must be non-decreasing, and elements can be reused (e.g., same index chosen multiple times). - The maximum XOR value for any three numbers within the range
[1, 1500]is achieved when all three numbers use the highest bits possible; 1500 in binary uses 11 bits (2¹¹ = 2048), so the maximum possible XOR is 2047 (all 11 bits set). - We iterate
ifrom 0 ton-1, thenjfromiton-1, computingpairXor = nums[i] ^ nums[j]once per(i, j)pair. - Then
kiterates fromjton-1, and we computetripletXor = pairXor ^ nums[k], marking it as seen. - This ensures all valid triplets are covered exactly once without redundant XOR computations.
- Finally, we scan the
seenarray to count distinct values.
Complexity Analysis
-
Time Complexity: O(n³) in the worst case due to three nested loops. With
n ≤ 1500, this can reach ~5.6×10⁸ operations in theory, but the actual number of triplets isn*(n+1)*(n+2)/6, which is about 563 million for n=1500. The precomputation of pair XORs saves a constant factor but does not change asymptotic complexity. -
Space Complexity: O(1) extra space besides the
seenarray of size 2048, which is constant and independent of input size.
Contact Links
If you found this series helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

If you want more helpful content like this, feel free to follow me:
Top comments (0)