Number of Unique XOR Triplets II - Problem

Given an integer array nums, you need to find all possible XOR triplets and count how many unique XOR values they produce.

A XOR triplet is defined as nums[i] ⊕ nums[j] ⊕ nums[k] where i ≤ j ≤ k. Note that the same element can be used multiple times if the indices satisfy the constraint.

Goal: Return the number of unique XOR triplet values from all possible triplets (i, j, k).

Example: For array [1, 2, 3], possible triplets are:

  • 1 ⊕ 1 ⊕ 1 = 1
  • 1 ⊕ 1 ⊕ 2 = 2
  • 1 ⊕ 1 ⊕ 3 = 3
  • 1 ⊕ 2 ⊕ 2 = 1
  • 1 ⊕ 2 ⊕ 3 = 0
  • And so on...

We only count the distinct XOR values, not how many times each appears.

Input & Output

example_1.py — Basic Case
$ Input: [1, 2, 3]
Output: 4
💡 Note: All possible triplets and their XOR values: (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: {0, 1, 2, 3}, so answer is 4.
example_2.py — Duplicates
$ Input: [1, 1, 1]
Output: 1
💡 Note: All triplets are (0,0,0), (0,0,1), (0,0,2), (0,1,1), (0,1,2), (0,2,2), (1,1,1), (1,1,2), (1,2,2), (2,2,2). Since all elements are 1, every XOR result is 1⊕1⊕1=1. Only one unique value: {1}.
example_3.py — Single Element
$ Input: [5]
Output: 1
💡 Note: Only one possible triplet: (0,0,0) which gives 5⊕5⊕5=5. Only one unique XOR value: {5}.

Visualization

Tap to expand
XOR Triplet Factory Assembly LineInput Hopper[1,2,3,1]Triplet Generator(i,j,k) combinationsXOR Processor⊕ ⊕ ⊕Unique Bin{0,1,2,3}Counter4Processing ExampleTriplet 1(0,0,0)1⊕1⊕1=1Triplet 2(0,1,2)1⊕2⊕3=0Triplet 3(0,0,1)1⊕1⊕2=2More...Continueprocessing🎯 Key Insight: Hash Set Automatically Handles Duplicates!Even if triplets (0,0,0), (1,1,1), and (3,3,3) all produce XOR=1,our unique bin counts it only once. Final answer = size of unique bin.
Understanding the Visualization
1
Setup Assembly Line
Initialize our processing station with a collection bin (hash set) for unique XOR values
2
Generate Triplets
Systematically create all valid triplet combinations where i ≤ j ≤ k
3
XOR Processing
Feed each triplet through the XOR processor: nums[i] ⊕ nums[j] ⊕ nums[k]
4
Unique Collection
Processed results automatically sorted into unique bins - duplicates discarded
5
Count Results
Final count represents the number of different XOR values we can create
Key Takeaway
🎯 Key Insight: The hash set automatically deduplicates XOR results, making the algorithm both simple and efficient. This is the optimal solution since we must examine all O(n³) triplet combinations.

Time & Space Complexity

Time Complexity
⏱️
O(n³)

Must consider all possible triplets - this is the theoretical minimum for this problem

n
2n
Quadratic Growth
Space Complexity
O(min(n³, 2^b))

Hash set stores unique XOR values, bounded by number of possible bit patterns

n
2n
Quadratic Space

Constraints

  • 1 ≤ nums.length ≤ 300
  • 1 ≤ nums[i] ≤ 108
  • Important: i ≤ j ≤ k constraint allows reuse of elements
Asked in
Google 23 Amazon 18 Meta 15 Microsoft 12
37.3K Views
Medium Frequency
~15 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen