LeetCode solutions

3704. Count Partitions with Even Sum Difference

My accepted Python solution to LeetCode problem 3704, Count Partitions with Even Sum Difference, running in 0ms.

  • Difficulty: Easy
  • Python
  • Runtime 0ms
  • Memory 17.4MB

Read the problem on LeetCode View on GitHub

Python

Accepted on LeetCode — runtime 0ms, memory 17.4MB, accepted 2025-12-30.

python
class Solution:
    def countPartitions(self, nums: List[int]) -> int:
        total = sum(nums)
        # Difference = left - right = left - (total - left) = 2*left - total
        # For difference to be even, 2*left - total must be even
        # 2*left is always even, so total must be even
        if total % 2 == 1:
            return 0
        # When total is even, all n-1 partitions give even difference
        return len(nums) - 1

Source