Ways to Split Array Into Three Subarrays - Problem
Array Partitioning Challenge: You need to find all the valid ways to split an array into exactly three contiguous subarrays such that their sums follow a specific order.

Given an array of non-negative integers, a "good" split must satisfy:
• Split into exactly 3 non-empty contiguous parts: left, mid, right
sum(left) ≤ sum(mid) ≤ sum(right)

Goal: Count all possible good splits. Since the answer can be large, return it modulo 10^9 + 7.

Example: For [1,1,1], there's only one way to split: [1] | [1] | [1] where 1 ≤ 1 ≤ 1

Input & Output

example_1.py — Basic Case
$ Input: nums = [1,1,1]
Output: 1
💡 Note: Only one way to split: [1] | [1] | [1]. Sum check: 1 ≤ 1 ≤ 1 ✅
example_2.py — Multiple Valid Splits
$ Input: nums = [1,2,2,2,5,0]
Output: 3
💡 Note: Valid splits: [1] | [2] | [2,2,5,0] (1≤2≤9), [1] | [2,2] | [2,5,0] (1≤4≤7), [1,2] | [2,2] | [5,0] (3≤4≤5)
example_3.py — No Valid Splits
$ Input: nums = [3,2,1]
Output: 0
💡 Note: Only possible split: [3] | [2] | [1]. Sum check: 3 ≤ 2 ≤ 1 ❌ (first condition fails)

Visualization

Tap to expand
Ways to Split Array Into Three SubarraysFind all valid ways where sum(left) ≤ sum(mid) ≤ sum(right)LEFTFixed sizesum ≤ midMIDDLEVariable rangeleft ≤ sum ≤ rightRIGHTRemainingsum ≥ midSplit 1Split 2Optimization Strategy1. Prefix Sums (O(n) preprocessing):Build cumulative sum array for O(1) subarray sum queries2. For each left size (O(n) iterations):Fix left boundary and find valid middle ranges3. Binary Search (2 × O(log n) per iteration):• Find minimum middle position where sum(mid) ≥ sum(left)• Find maximum middle position where sum(mid) ≤ sum(right)4. Count Range (O(1)):Add (max_pos - min_pos) to total countFinal ComplexityTime: O(n log n)Space: O(n)
Understanding the Visualization
1
Build Foundation
Create prefix sums for instant subarray sum calculation
2
Fix Left Team
For each possible left team size, find valid middle team ranges
3
Smart Search
Use binary search to efficiently find minimum and maximum valid middle positions
4
Count Solutions
Add the count of valid positions in the found range
Key Takeaway
🎯 Key Insight: Instead of checking every possible middle position (O(n²)), use binary search to find the valid range boundaries efficiently (O(n log n))!

Time & Space Complexity

Time Complexity
⏱️
O(n²)

Outer loop O(n) for left sizes, inner two-pointer search O(n) for each left

n
2n
Quadratic Growth
Space Complexity
O(n)

Prefix sum array requires O(n) additional space

n
2n
Linearithmic Space

Constraints

  • 3 ≤ nums.length ≤ 105
  • 0 ≤ nums[i] ≤ 104
  • All elements are non-negative integers
  • Array must be split into exactly 3 non-empty contiguous subarrays
Asked in
Google 45 Amazon 38 Meta 25 Microsoft 22
52.1K Views
Medium Frequency
~25 min Avg. Time
1.5K 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