Ways to Split Array
Problem
Given an integer array nums, return the number of ways to split the array into two non-empty parts such that the sum of the left part is greater than or equal to the sum of the right part.
- 2 ≤ nums.length ≤ 10⁵
- −10⁵ ≤ nums[i] ≤ 10⁵
Example
nums = [10,4,-8,7]2The array can be split at indices 0, 1, or 2. Splitting at index 0 yields left = [10], right = [4, -8, 7], sums 10 and 3, left >= right, valid. Splitting at index 1 yields left = [10,4], right = [-8,7], sums 14 and -1, valid. Splitting at index 2 yields left = [10,4,-8], right = [7], sums 6 and 7, invalid. Thus, total valid splits = 2.
Approach
Straightforward Solution
A naive approach checks every split index by summing left and right parts separately, resulting in O(n^2) time, which is too slow for large inputs.
Core Observation
The sum of any subarray can be computed efficiently using prefix sums, allowing constant-time queries for left and right sums after a single O(n) preprocessing pass.
Path to Optimal
By precomputing prefix sums, the sum of the left part at index i is prefix[i], and the right part sum is total sum minus prefix[i]. This reduces each split check to O(1), enabling an O(n) solution by iterating through all possible split points once.
Optimal Approach
PreviewCompute prefix sums in O(n). Iterate through indices from 0 to n-2, for each index compute left sum as prefix[i] and right sum as prefix[-1] - prefix[i]…
Full step-by-step walkthrough on Pro →
Want the full reasoning chain?
Unlock the complete walkthrough, line-by-line analysis, and recall drill.
Unlock ProTime
O(n)
Prefix sums are computed in a single pass (O(n)), and the split checks iterate once through the array (O(n)), resulting in overall O(n) time.
Space
O(n)
An auxiliary prefix sum array of size n is used to store cumulative sums, which scales linearly with input size.
Pattern Spotlight
Prefix Sum (Range Sum Queries)
Precompute cumulative sums to transform repeated range sum queries from O(n) each to O(1), enabling linear-time solutions for problems involving subarray sums.
Solution
| 1 | class Solution: |
| 2 | def waysToSplitArray(self, nums: List[int]) -> int: |
| 3 | n = len(nums) |
| 4 | |
| 5 | prefix = [nums[0]] |
| 6 | for i in range(1, n): |
| 7 | prefix.append(nums[i] + prefix[-1]) |
| 8 | |
| 9 | ans = 0 |
| 10 | for i in range(n - 1): |
| 11 | left_section = prefix[i] |
| 12 | right_section = prefix[-1] - prefix[i] |
| 13 | if left_section >= right_section: |
| 14 | ans += 1 |
| 15 | |
| 16 | return ans |
Step-by-Step Solution
Compute Prefix Sum Array to Enable Fast Sum Queries
| 3 | n = len(nums) |
| 5 | prefix = [nums[0]] |
| 6 | for i in range(1, n): |
| 7 | prefix.append(nums[i] + prefix[-1]) |
Objective
To build a prefix sum array where each element at index i represents the sum of nums from index 0 to i.
Key Insight
Prefix sums convert repeated sum queries into constant-time operations by storing cumulative sums. This preprocessing step is essential to avoid recomputing sums for every split, which would be prohibitively expensive.
Interview Quick-Check
Core Logic
Prefix sum array stores cumulative sums, enabling O(1) retrieval of any subarray sum by subtraction.
Common Pitfalls & Bugs
Forgetting to initialize prefix with the first element or incorrectly indexing can cause off-by-one errors.
Iterate Over Possible Split Points and Count Valid Splits
To iterate through all valid split indices and count how many satisfy the condition left sum >= right sum.
Return the Total Count of Valid Splits
To return the accumulated count of valid splits after processing all possible split points.
2 more steps with full analysis available on Pro.
Line Analysis
This solution has 1 Critical line interviewers watch for.
if left_section >= right_section:
Check if left section sum is greater than or equal to right section sum.
This condition directly implements the problem's requirement for a valid split.
Full line-by-line criticality + rationale for all 11 lines available on Pro.
Test Your Understanding
Why does using prefix sums reduce the time complexity from O(n^2) to O(n)?
See the answer with Pro.
Related Problems
Prefix Sum pattern
Don't just read it. Drill it.
Reconstruct Ways to Split Array from memory until it sticks. AlgoDrill blanks out key lines and makes you fill them back in, step by step.
Unlock the Ways to Split Array drill