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.
class Solution:
def countPartitions(self, nums: List[int]) -> int:
total = sum(nums)
return (len(nums) - 1) if total % 2 == 0 else 0- Time: O(n)
- Space: O(1)