Count Partitions with Even Sum Difference - Problem
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.
Input & Output
Example 1 — Basic Case
$
Input:
nums = [1,2,3,4]
›
Output:
3
💡 Note:
Partition at i=0: left=[1], right=[2,3,4], difference=|1-9|=8 (even). Partition at i=1: left=[1,2], right=[3,4], difference=|3-7|=4 (even). Partition at i=2: left=[1,2,3], right=[4], difference=|6-4|=2 (even). All 3 partitions have even differences, so count = 3.
Example 2 — Different Result
$
Input:
nums = [1,2,3]
›
Output:
2
💡 Note:
Partition at i=0: left=[1], right=[2,3], difference=|1-5|=4 (even). Partition at i=1: left=[1,2], right=[3], difference=|3-3|=0 (even). Both partitions have even differences, so count = 2.
Example 3 — All Odd Differences
$
Input:
nums = [1,2]
›
Output:
0
💡 Note:
Only one partition at i=0: left=[1], right=[2], difference=|1-2|=1 (odd). No partitions with even difference, so count = 0.
Constraints
- 2 ≤ nums.length ≤ 105
- -106 ≤ nums[i] ≤ 106
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code