[LeetCode] Count Partitions with Even Sum Difference

3432. Count Partitions with Even Sum Difference

You are given an integer array nums of length n.

A partition is defined as an index i where 0 <= i < n - 1, splitting the array into two non-empty subarrays such that:

  • Left subarray contains indices [0, i].
  • Right subarray contains indices [i + 1, n - 1].

Return the number of partitions where the difference between the sum of the left and right subarrays is even.

1
2
3
4
5
6
7
8
class Solution {
public:
int countPartitions(vector<int>& nums) {
int bit = 0;
for(auto& n : nums) bit ^= n;
return bit & 1 ? 0 : nums.size() - 1;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2025/01/26/PS/LeetCode/count-partitions-with-even-sum-difference/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.