Back to posts

LeetCode Challenge Day 81 — 3432. Count Partitions with Even Sum Difference

Nitin Ahirwal / December 5, 2025

LeetCode ChallengeDay 81MathParityPatternsJavaScriptEasy

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

  1. Compute the total sum S.
  2. If S is even, return n - 1.
  3. 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 👨‍💻