#3432
Easy Algorithms Count partitions with even sum difference
Array Math Prefix Sum
85.2% acceptance
Feb 25, 2026
403
12
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.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn count_partitions(nums: Vec<i32>) -> i32 {
let total: i32 = nums.iter().sum();
if total % 2 == 0 { (nums.len() - 1) as i32 } else { 0 }
}
}