leetcode

Leetcode Solutions (0x6A73)
git clone https://git.pngu.org/leetcode
Log | Files | Refs | Submodules | README

commit c21b21782861dde4d3d1744b91945addf8730c50
parent e2463525ffdacdfa6af3f1c7b9c29e248f695eff
Author: Jack Sangdahl <jack@pngu.org>
Date:   Fri,  5 Dec 2025 09:31:15 +0100

432. count partitions with even sum difference

Diffstat:
Acount_partition_even_sum_diff.c | 25+++++++++++++++++++++++++
Acount_partition_even_sum_diff.py | 29+++++++++++++++++++++++++++++
2 files changed, 54 insertions(+), 0 deletions(-)

diff --git a/count_partition_even_sum_diff.c b/count_partition_even_sum_diff.c @@ -0,0 +1,25 @@ +// 3432. Count Partitions with Even Sum Difference +#include "leetcode.h" + +/* + * 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]' and the + * 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. + */ + +int countPartitions(int *nums, int numsSize) { + int n = 1; + for (int i = 0; i < numsSize; i++) + n ^= nums[i]; + return (n & 1) * (numsSize - 1); +} + +int main() { + int n1[] = {10, 10, 3, 7, 6}, n2[] = {1, 2, 2}, n3[] = {2, 4, 6, 8}; + printf("%d\n", countPartitions(n1, ARRAY_SIZE(n1))); // expect: 4 + printf("%d\n", countPartitions(n2, ARRAY_SIZE(n2))); // expect: 0 + printf("%d\n", countPartitions(n3, ARRAY_SIZE(n3))); // expect: 3 +} diff --git a/count_partition_even_sum_diff.py b/count_partition_even_sum_diff.py @@ -0,0 +1,29 @@ +# 432. 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]' and the +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. +""" + + +class Solution(object): + def countPartitions(self, nums): + """ + :type nums: List[int] + :rtype: int + """ + parity = nums.pop() & 1 + for i in nums: + parity ^= i & 1 + return (parity ^ 1) * len(nums) + + +if __name__ == "__main__": + obj = Solution() + print(obj.countPartitions(nums=[10, 10, 3, 7, 6])) + print(obj.countPartitions(nums=[1, 2, 2])) + print(obj.countPartitions(nums=[2, 4, 6, 8]))