LeetCode Challenge Day 81 β 3432. Count Partitions with Even Sum Difference
Nitin Ahirwal / December 5, 2025
Hey folks π
This is Day 81 of my LeetCode streak π
Todayβs problem is 3432. Count Partitions with Even Sum Difference β a clean and elegant math-based problem.
π Problem Statement
You are given an integer array nums of length n.
A partition at index i splits the array into:
- Left subarray:
nums[0..i] - Right subarray:
nums[i+1..n-1]
A partition is valid if:
sum(left) - sum(right)
is even.
Return the number of valid partitions.
π‘ Intuition
Let:
- L = sum(left)
- R = sum(right)
- S = total sum = L + R
Then:
diff = L - R = 2L - S
Since 2L is always even, the parity of the result depends only on S.
- If S is even β all partitions result in an even difference β valid = n - 1
- If S is odd β no partition results in an even difference β valid = 0
This is the key observation that simplifies the problem completely.
π Approach
- Compute the total sum S.
- If S is even, return n - 1.
- If S is odd, return 0.
No prefix sums needed, no checking every index.
β±οΈ Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
π§βπ» Code (JavaScript)
/**
* @param {number[]} nums
* @return {number}
*/
var countPartitions = function(nums) {
const n = nums.length;
let total = 0;
for (let x of nums) total += x;
return (total % 2 === 0) ? (n - 1) : 0;
};
π§© Example Walkthrough
Input:
[10, 10, 3, 7, 6]
Total = 36 (even)
Valid partitions = 4
Output: 4
Input:
[1, 2, 2]
Total = 5 (odd)
Valid partitions = 0
Output: 0
π― Reflection
This problem shows how math can simplify logic-heavy tasks.
Instead of evaluating every partition, a simple parity check solves everything in constant time.
Thatβs it for Day 81 of my LeetCode challenge πͺ
See you tomorrow!
Happy Coding π¨βπ»