Number of Unique XOR Triplets II - Problem
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).
Input & Output
Example 1 — Basic Case
$
Input:
nums = [2,3,1,4]
›
Output:
8
💡 Note:
All possible XOR triplets: (0,0,0)→2, (0,0,1)→3, (0,0,2)→1, (0,0,3)→4, (0,1,1)→2, (0,1,2)→0, (0,1,3)→5, (0,2,2)→2, (0,2,3)→7, (0,3,3)→2, (1,1,1)→3, (1,1,2)→1, (1,1,3)→4, (1,2,2)→3, (1,2,3)→6, (1,3,3)→3, (2,2,2)→1, (2,2,3)→4, (2,3,3)→1, (3,3,3)→4. Unique values: {0,1,2,3,4,5,6,7} = 8 different values.
Example 2 — Small Array
$
Input:
nums = [1,2]
›
Output:
2
💡 Note:
All triplets: (0,0,0)→1, (0,0,1)→2, (0,1,1)→1, (1,1,1)→2. Unique values: {1,2} = 2 different values.
Example 3 — Identical Elements
$
Input:
nums = [5,5,5]
›
Output:
1
💡 Note:
All triplets use three 5's in various combinations, but since XOR of any odd number of identical values equals the value itself, all results equal 5. Unique values: {5} = 1 different value.
Constraints
- 3 ≤ nums.length ≤ 1000
- 0 ≤ nums[i] ≤ 216
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code