Find Xor-Beauty of Array - Problem
You are given a 0-indexed integer array nums.
The effective value of three indices i, j, and k is defined as ((nums[i] | nums[j]) & nums[k]).
The xor-beauty of the array is the XORing of the effective values of all the possible triplets of indices (i, j, k) where 0 <= i, j, k < n.
Return the xor-beauty of nums.
Note that:
val1 | val2is bitwise OR ofval1andval2.val1 & val2is bitwise AND ofval1andval2.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,4]
›
Output:
1
💡 Note:
All possible triplets: (0,0,0)→1, (0,0,1)→0, (0,1,0)→1, (0,1,1)→4, (1,0,0)→1, (1,0,1)→4, (1,1,0)→0, (1,1,1)→4. XOR result: 1⊕0⊕1⊕4⊕1⊕4⊕0⊕4 = 1
Example 2 — Single Element
$
Input:
nums = [15]
›
Output:
15
💡 Note:
Only one triplet possible: (0,0,0) gives (15|15)&15 = 15&15 = 15
Example 3 — Three Elements
$
Input:
nums = [1,2,3]
›
Output:
2
💡 Note:
Calculate all 27 possible triplets and XOR their effective values together
Constraints
- 1 ≤ nums.length ≤ 300
- 1 ≤ nums[i] ≤ 108
Visualization
Tap to expand
Understanding the Visualization
1
Input Array
Given array nums = [1,4]
2
Calculate Triplets
Find ((nums[i]|nums[j]) & nums[k]) for all i,j,k
3
XOR Results
XOR all effective values to get final answer
Key Takeaway
🎯 Key Insight: XOR properties cause paired terms to cancel, dramatically reducing computation complexity
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code