Split Array Into Maximum Number of Subarrays - Problem

You're given an array of non-negative integers and need to split it into subarrays in a very specific way!

Here's the challenge: Each subarray has a score calculated by performing a bitwise AND operation on all its elements. For example, if you have a subarray [5, 4, 1], its score would be 5 AND 4 AND 1 = 0.

Your goal is to:

  • Split the array into one or more contiguous subarrays
  • Make sure the sum of all subarray scores is minimized
  • Among all possible splits that achieve this minimum sum, find the one with the maximum number of subarrays

Think of it like this: you want to break the array into as many pieces as possible, but only if doing so doesn't increase the total score sum beyond the absolute minimum possible.

Example: For nums = [1, 0, 2, 0, 1, 2], you can split it as [1] [0] [2, 0, 1] [2] with scores 1 + 0 + 0 + 2 = 3, giving you 4 subarrays.

Input & Output

example_1.py โ€” Basic Splitting
$ Input: nums = [1,0,2,0,1,2]
โ€บ Output: 4
๐Ÿ’ก Note: We can split as [1],[0],[2,0,1],[2] with scores 1+0+0+2=3. Since the global AND is 0, this achieves the minimum sum with maximum subarrays.
example_2.py โ€” No Beneficial Splits
$ Input: nums = [5,7,1,3]
โ€บ Output: 1
๐Ÿ’ก Note: The global AND is 5&7&1&3=1. Since this is >0, splitting won't reduce the sum, so we keep the entire array as one subarray.
example_3.py โ€” All Zeros
$ Input: nums = [0,0,0]
โ€บ Output: 3
๐Ÿ’ก Note: Each element can form its own subarray with score 0, giving us 3 subarrays with total sum 0+0+0=0.

Constraints

  • 1 โ‰ค nums.length โ‰ค 104
  • 0 โ‰ค nums[i] โ‰ค 106
  • All elements are non-negative integers

Visualization

Tap to expand
Array Analysis: nums = [1, 0, 2, 0, 1, 2]Global AND = 1 & 0 & 2 & 0 & 1 & 2 = 0 โ† Splitting can help!Step 1: [1]Running AND = 1Continue...Step 2: [0]Running AND = 0SPLIT! โœ‚๏ธStep 3: [2,0,1]Running AND = 2&0&1 = 0SPLIT! โœ‚๏ธStep 4: [2]Running AND = 2End of array๐ŸŽฏ Optimal Result Analysisโœ“ Total subarrays: 4 (maximum possible)โœ“ Sum of scores: 1 + 0 + 0 + 2 = 3 (minimum possible)โœ“ Strategy: Split greedily when running AND = 0๐Ÿ’ก Key Insight: AND operations can only decrease values, so split whenever you hit 0!
Understanding the Visualization
1
Compute Global Baseline
Calculate AND of entire array to find the minimum achievable sum
2
Evaluate Split Benefit
If global AND > 0, splitting can't improve the sum, so return 1
3
Greedy Splitting Decision
When global AND = 0, split greedily whenever running AND reaches 0
4
Maximize Subarray Count
Each optimal split point increases our subarray count while maintaining minimum sum
Key Takeaway
๐ŸŽฏ Key Insight: The greedy approach works because bitwise AND can only decrease or maintain values. When the global AND is 0, we achieve the minimum sum by splitting whenever the running AND becomes 0, maximizing our subarray count.
Asked in
Google 45 Meta 32 Amazon 28 Microsoft 18
23.4K Views
Medium-High Frequency
~18 min Avg. Time
890 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