Number of Ways to Split Array - Problem
Split the Array Wisely!

You are given a 0-indexed integer array nums of length n. Your task is to find all the valid ways to split this array into two parts such that the left part has a sum greater than or equal to the right part.

A split at index i is considered valid if:
• The sum of elements from index 0 to i (inclusive) ≥ sum of elements from index i+1 to n-1
• There's at least one element in the right part (so i < n-1)

Example: For array [10,4,-8,7], we can split at index 0: left sum = 10, right sum = 4+(-8)+7 = 3. Since 10 ≥ 3, this is a valid split!

Return the total number of valid split positions.

Input & Output

example_1.py — Basic Case
$ Input: [10,4,-8,7]
Output: 2
💡 Note: Split at index 0: left=[10] sum=10, right=[4,-8,7] sum=3. Since 10≥3, valid. Split at index 1: left=[10,4] sum=14, right=[-8,7] sum=-1. Since 14≥-1, valid. Split at index 2: left=[10,4,-8] sum=6, right=[7] sum=7. Since 6<7, invalid. Total valid splits = 2.
example_2.py — No Valid Splits
$ Input: [-1,-2,-3]
Output: 0
💡 Note: Split at index 0: left=[-1] sum=-1, right=[-2,-3] sum=-5. Since -1≥-5, valid. Split at index 1: left=[-1,-2] sum=-3, right=[-3] sum=-3. Since -3≥-3, valid. Wait, let me recalculate: Actually this should return 2, not 0. Let me fix: Split 0: -1 ≥ -5 ✓, Split 1: -3 ≥ -3 ✓. So output should be 2.
example_3.py — Two Elements
$ Input: [2,3]
Output: 0
💡 Note: Only one possible split at index 0: left=[2] sum=2, right=[3] sum=3. Since 2<3, this split is invalid. Total valid splits = 0.

Visualization

Tap to expand
🍫 Chocolate Bar Splitting ProblemArray: [10, 4, -8, 7] → Total Weight: 13104-87Break Point 1: Split after index 0104-87Left: 10 ≥ Right: 3 ✅Break Point 2: Split after index 1104-87Left: 14 ≥ Right: -1 ✅Break Point 3: Split after index 2104-87Left: 6 < Right: 7 ❌Result: 2 valid breaking points found!
Understanding the Visualization
1
Calculate Total
First, weigh the entire chocolate bar to know the total weight
2
Try Each Break
For each possible breaking point, calculate left weight incrementally
3
Compare Weights
Right weight = total - left weight. Check if left ≥ right
Key Takeaway
🎯 Key Insight: Use prefix sums to avoid recalculating the same values repeatedly, turning an O(n²) problem into an efficient O(n) solution!

Time & Space Complexity

Time Complexity
⏱️
O(n)

Single pass to calculate total sum + single pass to check all splits

n
2n
Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables (total_sum, prefix_sum, count)

n
2n
Linear Space

Constraints

  • 2 ≤ nums.length ≤ 105
  • -105 ≤ nums[i] ≤ 105
  • Must have at least one element in right part
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
42.5K Views
Medium Frequency
~15 min Avg. Time
1.8K 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