Split Array with Equal Sum - Problem
Split Array with Equal Sum - The Quadruple Balance Challenge

You're given an integer array nums and need to find if it's possible to split it into four subarrays with equal sums using three strategic split points.

๐ŸŽฏ Your Mission: Find three indices i, j, k such that:
โ€ข 0 < i < i+1 < j < j+1 < k < n-1 (proper spacing between splits)
โ€ข All four resulting subarrays have identical sums

The four subarrays are:
1. [0...i-1] - Left segment
2. [i+1...j-1] - Left-middle segment
3. [j+1...k-1] - Right-middle segment
4. [k+1...n-1] - Right segment

Note that elements at positions i, j, and k act as separators and are not included in any subarray sum.

Input & Output

example_1.py โ€” Basic Valid Split
$ Input: [1,2,1,2,1,2,1]
โ€บ Output: true
๐Ÿ’ก Note: We can split at positions i=1, j=3, k=5. This gives us four segments: [1] (sum=1), [1] (sum=1), [1] (sum=1), [1] (sum=1). All segments have equal sum of 1.
example_2.py โ€” No Valid Split
$ Input: [1,2,3,4,5,6]
โ€บ Output: false
๐Ÿ’ก Note: No matter how we place the three separators, we cannot create four segments with equal sums. The array has an ascending pattern that makes equal splits impossible.
example_3.py โ€” Edge Case Too Short
$ Input: [1,1,1,1]
โ€บ Output: false
๐Ÿ’ก Note: The array is too short. We need at least 7 elements to place 3 separators with proper spacing constraints (0 < i, i+1 < j, j+1 < k < n-1).

Visualization

Tap to expand
The Perfect Balance Scale ProblemArray Elements (Weights on the Scale)1212121ij (fixed)kLeft SegmentsSegment 1: Sum = 1Segment 2: Sum = 1โœ“ Balanced!Right SegmentsSegment 3: Sum = 1Segment 4: Sum = 1โœ“ Balanced!Hash Set LookupLeft sum: 1 โˆˆ HashSetRight sum: 1 โˆˆ HashSetโœ“ Match Found!๐ŸŽฏ Algorithm Steps1. Fix middle separator j2. Find all balanced left pairs โ†’ store in hash set3. For each balanced right pair โ†’ check hash setTime: O(nยฒ) | Space: O(n)
Understanding the Visualization
1
Setup Prefix Sums
Create a cumulative weight system for instant segment weight calculation
2
Fix Middle Fulcrum
Choose the middle balance point j and work outward
3
Map Left Combinations
Find all left segment pairs that balance and store their weights
4
Match Right Combinations
For each balanced right pair, check if a matching left weight exists
Key Takeaway
๐ŸŽฏ Key Insight: By fixing the middle separator j, we transform a complex 3D search into efficient 2D matching using hash tables and prefix sums.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(nโด)

O(nยณ) for three nested loops ร— O(n) for calculating each segment sum

n
2n
โœ“ Linear Growth
Space Complexity
O(1)

Only using constant extra space for variables

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 2000
  • -106 โ‰ค nums[i] โ‰ค 106
  • Array must have at least 7 elements for valid split
  • Each separator position must maintain proper spacing
Asked in
Google 28 Amazon 19 Meta 15 Microsoft 12
28.4K Views
Medium-High Frequency
~25 min Avg. Time
847 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