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 with at least two numbers. Cut it between two neighbouring positions into a left part and a right part, each with at least one number. A cut is valid when the sum of the left part is greater than or equal to the sum of the right part. Return how many valid cuts there are. The array holds up to 100,000 numbers, each between -100,000 and 100,000.

Example 1
Input: nums = [10, 4, -8, 7]
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
Input: nums = [2, 3, 1, 0]
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.
Solution

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.

The array 10, 4, -8, 7 has total 13 and three places to cut it. After 10: left 10, right 3, valid. After 4: left 14, right -1, valid. After -8: left 6, right 7, not valid.

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 total before looking at any cut. Then, while the cut moves right, keep the left part's sum in left by adding the one number that just moved to the left. The right part's sum is total - left, with no second loop and no array of sums.

Where the cut can go

Which positions can the cut come after? A cut after position i puts positions 0 to i on the left. A cut after the last position would leave the right part empty, which is not allowed. So i runs from 0 to n - 2, which gives the n - 1 cuts.

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, not >. For [1, 1] the only cut gives 1 against 1, and it counts.

Adding without overflow

Can the sums get too big? A Java int holds values up to 2,147,483,647, about 2.1 billion. With 100,000 numbers of 100,000, the total reaches 10 billion. An int that goes past its largest value continues from the most negative values, which is called overflow. Keeping total and left in long avoids it, since a long holds values up to about 9.2 × 1018.

Java Solution

The method waysToSplitArray works in these steps:

  1. First, add every number of nums into total, a long, with one loop over the array.
  2. Then move the cut from left to right with a second loop, over i from 0 while i < nums.length - 1, so the right part is never empty. Each step adds nums[i] to left, the left part's sum, also a long.
  3. Compare left with total - left, the right part's sum. If left is greater or equal, add one to the counter ways.
  4. When the loop is done, return ways.

The main method runs both examples, [1, 1] with its tied cut, [-3, 1, 1] where no cut is valid, and 50,000 numbers whose left sum goes past the int range: 25,000 copies of 100,000, then 25,000 copies of -100,000.

Java
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:

Output
2
2
1
0
49999

Both examples print 2, as worked out by hand. For [10, 4, -8, 7], that 2 counts the cuts after 10 and after 4 from the picture. The 1 on the third line is the tied cut of [1, 1]. With > in place of >=, that line prints 0. The fourth line is 0 because [-3, 1, 1] gives -3 against 2 and then -2 against 1, and the left part never catches up.

The last line, 49999, counts every one of the 49,999 cuts of the big array. Its total is 0, and left stays at 100,000 or more, so the left part always wins. On the way, left climbs to 2,500,000,000, past the int range. The same method with int sums lets left overflow to negative values there, and it prints 42948 for this array.

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, left and ways are kept, and no array of prefix sums is built.

The same pairing of a left sum with "total minus left" finds the balance point of an array in Find Pivot Index.