Number of Ways to Split Array
Count the places where an array can be cut so that its left part adds up to at least its right part.
You are given an array of integers nums
Example 1
Output: 2
Explanation: cutting after 10 gives 10 against 3, after 4 gives 14 against -1, and after -8 gives 6 against 7. The first two cuts are valid.
Example 2
Output: 2
Explanation: cutting after 2 gives 2 against 4, after 3 gives 5 against 1, and after 1 gives 6 against 0. The last two cuts are valid.
The Idea
Think of a row of cards with numbers on them, some positive and some negative. You slide a divider in between two cards, and the cards on its left go to one side, the rest to the other. The left side wins when its cards add up to at least as much as the right side's cards.
Take the first example, the cards 10, 4, -8 and 7. Four cards have three gaps between them, so there are three places for the divider. Look at the picture below. Each row puts the divider, the orange line, in a different gap. The blue cards are the left part and the green ones the right part.
In the first row the left part is just 10, and the right part is 4 + (-8) + 7 = 3, so the cut is valid. In the second row the left part is 14 and the right part -1, valid again. In the last row the left part drops to 6 because of the -8, and the right part, 7, is bigger. That makes 2 valid cuts.
Adding up both parts again for every row works, but is it needed? Look at the top of the picture: all four cards add up to 13, wherever the divider stands. Whatever is not on the left is on the right, so the right part is always 13 minus the left part: 13 - 10 = 3, 13 - 14 = -1, 13 - 6 = 7.
And the left part changes by one card from row to row. Moving the divider one gap to the right adds one card to the left part: 10, then 10 + 4 = 14, then 14 + (-8) = 6. So one total and one sum that grows as we go are enough to check every cut. A sum of the first few numbers of an array is called a prefix sum, and keeping it up to date while walking through the array is called a running sum.
Solution Approach
Turning that idea into code means settling four things: how to get the right part's sum, where the cut may go, whether equal sums count, and which type to add the numbers in.
Getting the right part's sum
Why not simply add up both parts at every cut? Each cut then costs about as many additions as there are numbers, and an array of n numbers has n - 1 cuts. For 100,000 numbers that is about 10 billion additions.
Instead, add all the numbers once into totallefttotal - left
Where the cut can go
Which positions can the cut come after? A cut after position iii
Equal sums
What about a cut where both parts add up to the same value? The problem asks for a left sum greater than or equal to the right sum, so a tie is valid. The comparison is left >= total - left>[1, 1]
Adding without overflow
Can the sums get too big? A Java intinttotalleftlonglong
Java Solution
The method waysToSplitArray
- First, add every number of
intonums , atotal , with one loop over the array.long - Then move the cut from left to right with a second loop, over
from 0 whilei , so the right part is never empty. Each step addsi < nums.length - 1 tonums[i] , the left part's sum, also aleft .long - Compare
withleft , the right part's sum. Iftotal - left is greater or equal, add one to the counterleft .ways - When the loop is done, return
.ways
The main[1, 1][-3, 1, 1]int
public class NumberOfWaysToSplitArray {
public static int waysToSplitArray(int[] nums) {
long total = 0;
for (int x : nums) {
total += x; // long: the sum can pass the int range
}
long left = 0;
int ways = 0;
for (int i = 0; i < nums.length - 1; i++) { // the right part must not be empty
left += nums[i];
if (left >= total - left) { // total - left is the right part's sum
ways++;
}
}
return ways;
}
public static void main(String[] args) {
System.out.println(waysToSplitArray(new int[] {10, 4, -8, 7}));
System.out.println(waysToSplitArray(new int[] {2, 3, 1, 0}));
System.out.println(waysToSplitArray(new int[] {1, 1}));
System.out.println(waysToSplitArray(new int[] {-3, 1, 1}));
int[] big = new int[50000]; // 25,000 times 100,000, then 25,000 times -100,000
for (int i = 0; i < big.length; i++) {
big[i] = i < 25000 ? 100000 : -100000;
}
System.out.println(waysToSplitArray(big));
}
}
Running it prints:
2
2
1
0
49999
Both examples print 2, as worked out by hand. For [10, 4, -8, 7][1, 1]>>=[-3, 1, 1]
The last line, 49999, counts every one of the 49,999 cuts of the big array. Its total is 0, and leftleftintintleft
Complexity
The method reads the array twice, once for the total and once for the cuts, and keeps only a few numbers.
- Time: O(n), for an array of n numbers, because the first loop adds all n numbers once, and the second does one addition and one comparison for each of the n - 1 cuts.
- Space: O(1). Only
,total andleft are kept, and no array of prefix sums is built.ways
The same pairing of a left sum with "total minus left" finds the balance point of an array in