3432. Count Partitions with Even Sum Difference LeetCode Solution

In this guide, you will get 3432. Count Partitions with Even Sum Difference LeetCode Solution with the best time and space complexity. The solution to Count Partitions with Even Sum Difference problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Count Partitions with Even Sum Difference solution in C++
  4. Count Partitions with Even Sum Difference solution in Java
  5. Count Partitions with Even Sum Difference solution in Python
  6. Additional Resources
3432. Count Partitions with Even Sum Difference LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: nums = [10,10,3,7,6]
Output: 4
Explanation:
The 4 partitions are:

[10], [10, 3, 7, 6] with a sum difference of 10 – 26 = -16, which is even.
[10, 10], [3, 7, 6] with a sum difference of 20 – 16 = 4, which is even.
[10, 10, 3], [7, 6] with a sum difference of 23 – 13 = 10, which is even.
[10, 10, 3, 7], [6] with a sum difference of 30 – 6 = 24, which is even.

See also  3412. Find Mirror Score of a String LeetCode Solution

Example 2:

Input: nums = [1,2,2]
Output: 0
Explanation:
No partition results in an even sum difference.

Example 3:

Input: nums = [2,4,6,8]
Output: 3
Explanation:
All partitions result in an even sum difference.

Constraints:

2 <= n == nums.length <= 100
1 <= nums[i] <= 100

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

3432. Count Partitions with Even Sum Difference LeetCode Solution in C++

class Solution {
 public:
  int countPartitions(std::vector<int>& nums) {
    // If we add the same number in the left subarray and remove it from the
    // right subarray, then the difference remains the same parity. So, just
    // return the number of ways to partition the array into two subarrays when
    // the array sum is even.
    return accumulate(nums.begin(), nums.end(), 0) % 2 == 0 ? nums.size() - 1
                                                            : 0;
  }
};
/* code provided by PROGIEZ */

3432. Count Partitions with Even Sum Difference LeetCode Solution in Java

class Solution {
  public int countPartitions(int[] nums) {
    // If we add the same number in the left subarray and remove it from the
    // right subarray, then the difference remains the same parity. So, just
    // return the number of ways to partition the array into two subarrays when
    // the array sum is even.
    return Arrays.stream(nums).sum() % 2 == 0 ? nums.length - 1 : 0;
  }
}
// code provided by PROGIEZ

3432. Count Partitions with Even Sum Difference LeetCode Solution in Python

class Solution:
  def countPartitions(self, nums: list[int]) -> int:
    # If we add the same number in the left subarray and remove it from the
    # right subarray, then the difference remains the same parity. So, just
    # return the number of ways to partition the array into two subarrays when
    # the array sum is even.
    return len(nums) - 1 if sum(nums) % 2 == 0 else 0
# code by PROGIEZ

Additional Resources

See also  1045. Customers Who Bought All Products LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.