Maximum Number of Ways to Partition an Array Problem
Description
LeetCode Problem 2025.
You are given a 0-indexed integer array nums of length n. The number of ways to partition nums is the number of pivot indices that satisfy both conditions:
1 <= pivot < nnums[0] + nums[1] + ... + nums[pivot - 1] == nums[pivot] + nums[pivot + 1] + ... + nums[n - 1]
You are also given an integer k. You can choose to change the value of one element of nums to k, or to leave the array unchanged.
Return the maximum possible number of ways to partition nums to satisfy both conditions after changing at most one element.
Example 1:
Input: nums = [2,-1,2], k = 3
Output: 1
Explanation: One optimal approach is to change nums[0] to k. The array becomes [3,-1,2].
There is one way to partition the array:
- For pivot = 2, we have the partition [3,-1 | 2]: 3 + -1 == 2.
Example 2:
Input: nums = [0,0,0], k = 1
Output: 2
Explanation: The optimal approach is to leave the array unchanged.
There are two ways to partition the array:
- For pivot = 1, we have the partition [0 | 0,0]: 0 == 0 + 0.
- For pivot = 2, we have the partition [0,0 | 0]: 0 + 0 == 0.
Example 3:
Input: nums = [22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14], k = -33
Output: 4
Explanation: One optimal approach is to change nums[2] to k. The array becomes [22,4,-33,-20,-15,15,-16,7,19,-10,0,-13,-14].
There are four ways to partition the array.
Constraints:
n == nums.length2 <= n <= 10^5-10^5 <= k, nums[i] <= 10^5
Solution
Approach: Prefix Sums + Hash Maps
Let s[i] be the prefix sum up to index i and total the sum of the whole array. A split after index i is valid when s[i] = total - s[i], i.e. s[i] = total / 2.
Without changing anything, the answer is the number of split points i < n - 1 with s[i] = total / 2 (possible only when total is even). Keep a map right that counts the prefix sums at all split points.
If we change nums[i] to k, the total changes by d = k - nums[i]. A split at a position before i keeps its left sum, so it needs s = (total + d) / 2. A split at a position at or after i has both its left sum and the total increased by d, which gives s = (total - d) / 2 for the original prefix sum. So for each i the number of valid splits is left[(total + d) / 2] + right[(total - d) / 2], where left counts the prefix sums at split points before i and right those from i on (both only when total + d is even). While scanning i from left to right, move each prefix sum from right to left. The answer is the best count over all changes and the no-change count.
Complexity: O(n) time and O(n) space.
from collections import defaultdict
class Solution:
def waysToPartition(self, nums: List[int], k: int) -> int:
n = len(nums)
s = [nums[0]] * n
right = defaultdict(int)
for i in range(1, n):
s[i] = s[i - 1] + nums[i]
right[s[i - 1]] += 1
ans = 0
if s[-1] % 2 == 0:
ans = right[s[-1] // 2]
left = defaultdict(int)
for v, x in zip(s, nums):
d = k - x
if (s[-1] + d) % 2 == 0:
t = left[(s[-1] + d) // 2] + right[(s[-1] - d) // 2]
if ans < t:
ans = t
left[v] += 1
right[v] -= 1
return ans