Number of Unique XOR Triplets I - Problem
You are given an integer array nums of length n, where nums is a permutation of the numbers in the range [1, n].
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 = [1,2,3]
›
Output:
4
💡 Note:
All possible triplets: (0,0,0):1⊕1⊕1=1, (0,0,1):1⊕1⊕2=2, (0,0,2):1⊕1⊕3=3, (0,1,1):1⊕2⊕2=1, (0,1,2):1⊕2⊕3=0, (0,2,2):1⊕3⊕3=1, (1,1,1):2⊕2⊕2=2, (1,1,2):2⊕2⊕3=3, (1,2,2):2⊕3⊕3=2, (2,2,2):3⊕3⊕3=3. Unique values are {0,1,2,3}, so answer is 4.
Example 2 — Smaller Array
$
Input:
nums = [2,1]
›
Output:
2
💡 Note:
Possible triplets: (0,0,0):2⊕2⊕2=2, (0,0,1):2⊕2⊕1=1, (0,1,1):2⊕1⊕1=2, (1,1,1):1⊕1⊕1=1. Unique values are {1,2}, so answer is 2.
Example 3 — Single Element
$
Input:
nums = [1]
›
Output:
1
💡 Note:
Only one triplet possible: (0,0,0):1⊕1⊕1=1. Unique values are {1}, so answer is 1.
Constraints
- 1 ≤ nums.length ≤ 300
- nums is a permutation of [1, 2, ..., n]
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code