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 | val2 is bitwise OR of val1 and val2.
  • val1 & val2 is bitwise AND of val1 and val2.

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
XOR Beauty: Calculate All Effective Values14For each triplet (i,j,k): calculate (nums[i] | nums[j]) & nums[k]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)→4XOR all: 1⊕0⊕1⊕4⊕1⊕4⊕0⊕4Final Result: 1
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
Asked in
Google 15 Microsoft 12
12.0K Views
Medium Frequency
~25 min Avg. Time
580 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