Count how many distinct XOR values can be formed by choosing three indices from an array.
Number of Unique XOR Triplets II
gfgProblem
Given an integer array nums, consider every triplet of indices (i, j, k) with i < j < k. For each triplet, compute the XOR value nums[i] ^ nums[j] ^ nums[k].
Return the number of distinct XOR values that can appear among all such triplets.
In other words, different index triplets may produce the same XOR value; count each resulting value only once.
Input Format
- An integer array
nums. - Each element of
numsis used as an array value. - A valid triplet must choose three different indices
i < j < k.
Output Format
- Return an integer representing the number of unique XOR results over all valid triplets.
Constraints
3 <= nums.length- Values are integers in a range suitable for bitwise XOR operations.
- The exact official constraints are not provided here; an efficient solution should avoid enumerating all triplets when possible.
Example 1
Input
nums = [1, 2, 3]
Output
1
Explanation
Only one triplet exists: (0, 1, 2). Its XOR is 1 ^ 2 ^ 3 = 0, so there is exactly one distinct result.
Example 2
Input
nums = [1, 1, 2, 2]
Output
2
Explanation
The possible triplets can produce XOR values 1 or 2, but no other distinct result appears.
Premium problem context
Unlock deeper context for this problem
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.