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 πŸ‘¨β€πŸ’»