Count the Number of Beautiful Subarrays - Problem
Count the Number of Beautiful Subarrays
You are given a
The operation allows you to:
• Choose any two different indices
• Select a bit position
• Subtract
A subarray is beautiful if you can reduce all its elements to zero using this operation any number of times. Note that subarrays with all elements initially zero are automatically beautiful.
Goal: Return the total count of beautiful subarrays in the given array.
Key Insight: This problem is secretly about XOR operations! A subarray can be reduced to all zeros if and only if the XOR of all its elements equals zero.
You are given a
0-indexed integer array nums. Your task is to find all beautiful subarrays - contiguous subsequences where you can make all elements equal to zero using a special bit manipulation operation.The operation allows you to:
• Choose any two different indices
i and j in the current subarray• Select a bit position
k where both nums[i] and nums[j] have their k-th bit set to 1• Subtract
2^k from both numbers simultaneouslyA subarray is beautiful if you can reduce all its elements to zero using this operation any number of times. Note that subarrays with all elements initially zero are automatically beautiful.
Goal: Return the total count of beautiful subarrays in the given array.
Key Insight: This problem is secretly about XOR operations! A subarray can be reduced to all zeros if and only if the XOR of all its elements equals zero.
Input & Output
example_1.py — Basic Case
$
Input:
nums = [4, 2, 1, 3]
›
Output:
2
💡 Note:
Beautiful subarrays are [4,2,1,3] (XOR = 4⊕2⊕1⊕3 = 0) and [2,1,3] (XOR = 2⊕1⊕3 = 0). The key insight is that these subarrays can be reduced to all zeros using the bit manipulation operations.
example_2.py — All Zeros
$
Input:
nums = [1, 10, 4]
›
Output:
0
💡 Note:
No subarray has XOR equal to 0. For example: [1] has XOR=1, [10] has XOR=10, [4] has XOR=4, [1,10] has XOR=11, [10,4] has XOR=14, [1,10,4] has XOR=15. None can be made all zeros.
example_3.py — Single Element Zero
$
Input:
nums = [0]
›
Output:
1
💡 Note:
The single subarray [0] is beautiful since it's already all zeros and no operations are needed.
Visualization
Tap to expand
Understanding the Visualization
1
Understand XOR
XOR operation: same bits cancel (1⊕1=0, 0⊕0=0), different bits remain (1⊕0=1)
2
Prefix XOR Insight
If prefix_xor[i] = prefix_xor[j], then subarray (i,j] has XOR = 0
3
Hash Table Optimization
Store frequency of each prefix XOR value seen so far
4
Count Beautiful Subarrays
For each position, add the frequency of current prefix XOR to result
Key Takeaway
🎯 Key Insight: Beautiful subarrays are those with XOR = 0. Use prefix XOR and hash table to find them efficiently in O(n) time!
Time & Space Complexity
Time Complexity
O(n)
Single pass through array with O(1) hash map operations
✓ Linear Growth
Space Complexity
O(n)
Hash map can store up to n different prefix XOR values
⚡ Linearithmic Space
Constraints
- 1 ≤ nums.length ≤ 105
- 0 ≤ nums[i] ≤ 106
- All subarrays are contiguous sequences
- XOR operation is associative and commutative
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code