Count Partitions with Even Sum Difference
Source: leetcode
Difficulty: Easy
Tags: array, math, prefix-sum

You are given an integer array `nums` of length `n`.


A **partition** is defined as an index `i` where `0 <= i < n - 1`, splitting the array into two **non\-empty** subarrays such that:


- Left subarray contains indices `[0, i]`.
- Right subarray contains indices `[i + 1, n - 1]`.


Return the number of **partitions** where the **difference** between the **sum** of the left and right subarrays is **even**.


 


**Example 1:**



**Input:** nums \= \[10,10,3,7,6]


**Output:** 4


**Explanation:**


The 4 partitions are:


- `[10]`, `[10, 3, 7, 6]` with a sum difference of `10 - 26 = -16`, which is even.
- `[10, 10]`, `[3, 7, 6]` with a sum difference of `20 - 16 = 4`, which is even.
- `[10, 10, 3]`, `[7, 6]` with a sum difference of `23 - 13 = 10`, which is even.
- `[10, 10, 3, 7]`, `[6]` with a sum difference of `30 - 6 = 24`, which is even.



**Example 2:**



**Input:** nums \= \[1,2,2]


**Output:** 0


**Explanation:**


No partition results in an even sum difference.



**Example 3:**



**Input:** nums \= \[2,4,6,8]


**Output:** 3


**Explanation:**


All partitions result in an even sum difference.



 


**Constraints:**


- `2 <= n == nums.length <= 100`
- `1 <= nums[i] <= 100`




C++ solution:
class Solution {
public:
    int countPartitions(vector<int>& nums) {
        int total = 0;
        for (int num : nums) {
            total += num;
        }
        if (total % 2 != 0) {
            return 0;
        }
        return nums.size() - 1;
    }
};

Go solution:
func countPartitions(nums []int) int {
    total := 0
    for _, num := range nums {
        total += num
    }
    if total % 2 != 0 {
        return 0
    }
    return len(nums) - 1
}

Java solution:
class Solution {
    public int countPartitions(int[] nums) {
        int total = 0;
        for (int num : nums) {
            total += num;
        }
        return total % 2 == 0 ? nums.length - 1 : 0;
    }
}

JavaScript solution:
var countPartitions = function(nums) {
    let total = 0;
    for (let num of nums) {
        total += num;
    }
    return total % 2 === 0 ? nums.length - 1 : 0;
};

Python solution:
class Solution:
    def countPartitions(self, nums: list) -> int:
        if sum(nums) % 2 != 0:
            return 0
        return len(nums) - 1

Ruby solution:
def count_partitions(nums)
  total = nums.sum
  total.even? ? nums.length - 1 : 0
end