Count the Number of Beautiful Subarrays - Problem
Count the Number of Beautiful Subarrays

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 simultaneously

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.

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
The XOR Pairing Magic 🪙Key Insight: XOR Properties• Same values cancel: 5 ⊕ 5 = 0• Zero is neutral: x ⊕ 0 = x• If A ⊕ B ⊕ C ⊕ D = 0, then all bits are perfectly paired!Beautiful subarray ↔ XOR equals 0Array: [4, 2, 1, 3]4213Prefix XOR:0→4→6→7→4[2,1,3] XOR = 0[4,2,1,3] XOR = 0Algorithm Steps1. Initialize: map = {0: 1}, prefix_xor = 02. Process 4: prefix_xor = 4, result += map[4] = 0 Update: map[4] = 13. Process 2: prefix_xor = 6, result += map[6] = 0 Update: map[6] = 14. Process 1: prefix_xor = 7, result += map[7] = 0 Update: map[7] = 15. Process 3: prefix_xor = 4, result += map[4] = 1! Found beautiful subarray: [2,1,3]🎯 Key Insight: When prefix XOR repeats, we've found a zero-XOR subarray!Time: O(n) | Space: O(n) | Beautiful subarrays: 2
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

n
2n
Linear Growth
Space Complexity
O(n)

Hash map can store up to n different prefix XOR values

n
2n
Linearithmic Space

Constraints

  • 1 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 106
  • All subarrays are contiguous sequences
  • XOR operation is associative and commutative
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
28.4K Views
Medium-High Frequency
~25 min Avg. Time
892 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