Skip to content

Instantly share code, notes, and snippets.

@Ifihan
Created December 5, 2025 22:28
Show Gist options
  • Select an option

  • Save Ifihan/f728d544ca9522e66e595b13e8ccea92 to your computer and use it in GitHub Desktop.

Select an option

Save Ifihan/f728d544ca9522e66e595b13e8ccea92 to your computer and use it in GitHub Desktop.
Count Partitions with Even Sum Difference

Question

Approach

I notice that the parity of the difference sum_left - sum_right equals the parity of sum_left + sum_right (because subtraction ≡ addition mod 2), and sum_left + sum_right is just the total sum of the array. So the difference is even exactly when the total sum is even.

Implementation

class Solution:
    def countPartitions(self, nums: List[int]) -> int:
        total = sum(nums)
        return (len(nums) - 1) if total % 2 == 0 else 0

Complexities

  • Time: O(n)
  • Space: O(1)
image
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment