Count Partitions with Even Sum Difference (LeetCode 3432): Solution and Explanation

Easy
Array
Math
Prefix Sum

Split an integer array into a non-empty left part and a non-empty right part at every possible point, and count the splits where left sum minus right sum is even.

Intuition

There are n - 1 places to put the divider. For each one you need the left sum and the right sum.

Recomputing both sums for every split would be wasteful. Compute the total once, keep a running left sum, and get the right sum as total minus left.

Now look at the difference: left - right = left - (total - left) = total - 2 * left. Subtracting an even number never changes parity, so the difference has the same parity as the total.

That means the answer is all-or-nothing: n - 1 when the total is even, and 0 when it is odd. The scan just confirms it split by split.

Approach: Running prefix sum

  1. Compute total = sum(nums) and set left = 0, count = 0.
  2. For each split index i from 0 to n - 2, add nums[i] to left.
  3. Set right = total - left.
  4. If (left - right) % 2 == 0, increase count.
  5. Return count.

Step-by-step example

nums = [10,10,3,7,6]
  1. total = 36, which is even.
  2. Split after index 0: left = 10, right = 26, difference -16 is even. count = 1.
  3. Split after index 1: left = 20, right = 16, difference 4 is even. count = 2.
  4. Split after index 2: left = 23, right = 13, difference 10 is even. count = 3.
  5. Split after index 3: left = 30, right = 6, difference 24 is even. count = 4. Return 4.

Code: Python, Java, C++

1class Solution:
2    def countPartitions(self, nums: List[int]) -> int:
3        total = sum(nums)
4        left = 0
5        count = 0
6        for i in range(len(nums) - 1):
7            left += nums[i]
8            right = total - left
9            if (left - right) % 2 == 0:
10                count += 1
11        return count
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

One pass to compute the total and one pass to slide the split.

Space

O(1)

Only a few running sums are stored.

Other approaches

Parity shortcut (time O(n), space O(1))

Return n - 1 if the total is even and 0 otherwise. It follows directly from left - right = total - 2 * left.

Brute force sums (time O(n^2), space O(1))

For each split, sum the left and right parts from scratch. Easy to write, but every split costs O(n).

Common mistakes

  • Letting the split run to index n - 1. That would leave the right part empty, which is not a valid partition.
  • Checking (left - right) % 2 == 1 for odd in languages where the remainder of a negative number is negative. Test for == 0 instead.
  • Recomputing the right sum with a loop every time, which turns a linear problem into a quadratic one.
  • Returning n instead of n - 1 when the total is even. There are only n - 1 split points.

Frequently asked questions

Why does the parity of the total decide everything?

The difference is total - 2 * left. Twice anything is even, so the difference is even exactly when the total is even, no matter where the split is.

Is the difference ever negative, and does that matter?

Yes, when the right part is heavier. A negative number is still even or odd in the usual way, so checking the remainder against 0 works in every language.

Can the answer be anything other than 0 or n - 1?

No. Every split has the same parity, so either all n - 1 splits count or none do.