Number of Ways to Split Array (LeetCode 2270): Solution and Explanation
Given an integer array nums, count the indices i with 0 <= i < n - 1 where the sum of nums[0..i] is greater than or equal to the sum of nums[i+1..n-1].
Intuition
Each split divides the array into a left part and a right part, and we compare their sums. Recomputing both sums for every split repeats almost all of the work, giving O(n^2).
Moving the split one step to the right changes very little: exactly one element leaves the right part and joins the left part. So the left sum grows by that element and the right sum shrinks by it.
Even simpler, the right sum is always the total minus the left sum. Compute the total once, keep a running left sum, and each split becomes a single comparison.
Approach: Running prefix sum against the total
- Compute
total = sum(nums)and setleft = 0,ways = 0. - For each
ifrom 0 ton - 2, addnums[i]toleft. - If
left >= total - left, the split afteriis valid, so incrementways. - Return
ways. Stopping atn - 2keeps the right part non-empty.
Step-by-step example
nums = [10,4,-8,7]
- total = 13.
- i = 0: left = 10, right = 3. 10 >= 3, so ways = 1.
- i = 1: left = 14, right = -1. 14 >= -1, so ways = 2.
- i = 2: left = 6, right = 7. 6 < 7, so this split is not valid.
- The loop stops before the last element. The answer is 2.
Code: Python, Java, C++
1class Solution:
2 def waysToSplitArray(self, nums: List[int]) -> int:
3 total = sum(nums)
4 left = 0
5 ways = 0
6 for i in range(len(nums) - 1):
7 left += nums[i]
8 if left >= total - left:
9 ways += 1
10 return ways1class Solution {
2 public int waysToSplitArray(int[] nums) {
3 long total = 0;
4 for (int x : nums) total += x;
5 long left = 0;
6 int ways = 0;
7 for (int i = 0; i < nums.length - 1; i++) {
8 left += nums[i];
9 if (left >= total - left) {
10 ways++;
11 }
12 }
13 return ways;
14 }
15}1class Solution {
2public:
3 int waysToSplitArray(vector<int>& nums) {
4 long long total = accumulate(nums.begin(), nums.end(), 0LL);
5 long long left = 0;
6 int ways = 0;
7 for (int i = 0; i + 1 < (int)nums.size(); i++) {
8 left += nums[i];
9 if (left >= total - left) {
10 ways++;
11 }
12 }
13 return ways;
14 }
15};Time and space complexity
O(n)
One pass to compute the total and one pass over the split positions.
O(1)
Only the total, the running left sum and the counter are stored.
Other approaches
Recompute both sums per split (time O(n^2), space O(1))
For every split, sum the left and right parts from scratch. Correct but quadratic, which times out at n = 10^5.
Prefix sum array (time O(n), space O(n))
Build prefix[i] = sum of the first i elements, then compare prefix[i + 1] with prefix[n] - prefix[i + 1]. Same speed, but it stores n + 1 extra values.
Common mistakes
- Overflowing a 32-bit integer. With 10^5 elements of magnitude up to 10^5, sums reach 10^10, so use long, long long, int64 or Python ints.
- Letting the loop reach the last index. A split needs a non-empty right part, so i stops at n - 2.
- Using > instead of >=. A split where both sides are equal is valid.
- Assuming the sums only grow. Elements can be negative, so the left sum can go down as the split moves right, and valid splits need not form one contiguous block.
Frequently asked questions
Why is the right sum total minus left?
Every element is in exactly one of the two parts, so left + right = total for every split.
Why do I need a 64-bit type if the function returns an int?
The answer is at most n - 1, which fits easily. The intermediate sums do not: they can be as large as 10^10 in magnitude.
Can the valid splits be anywhere, or only at the end?
Anywhere. With negative numbers the left sum can rise and fall, so valid and invalid splits can alternate.