You are given a 0-indexed integer array nums of length n. The number of ways to partitionnums is the number of pivot indices that satisfy both conditions:
Input: nums =[2,-1,2], k =3Output: 1Explanation: 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.
Input: nums =[0,0,0], k =1Output: 2Explanation: 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.
Input: nums =[22,4,-25,-20,-15,15,-16,7,19,-10,0,-13,-14], k =-33Output: 4Explanation: 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.
We want to count the number of ways to split the array into two parts with equal sum, possibly after changing one element to k. We can precompute prefix sums and use hash maps to efficiently count valid partitions before and after a change.
classSolution {
public:int waysToPartition(vector<int>& nums, int k) {
int n = nums.size();
longlong total =0;
for (int x : nums) total += x;
vector<longlong> pre(n);
pre[0] = nums[0];
for (int i =1; i < n; ++i) pre[i] = pre[i-1] + nums[i];
unordered_map<longlong, int> left, right;
for (int i =0; i < n-1; ++i) right[pre[i]]++;
int ans = right[total/2];
for (int i =0; i < n; ++i) {
longlong diff = k - nums[i];
longlong new_total = total + diff;
if (new_total %2!=0) continue;
longlong half = new_total /2;
int cnt =0;
if (right.count(half - diff)) cnt += right[half - diff];
if (left.count(half)) cnt += left[half];
ans = max(ans, cnt);
if (i < n-1) {
right[pre[i]]--;
left[pre[i]]++;
}
}
return ans;
}
};
classSolution {
publicintwaysToPartition(int[] nums, int k) {
int n = nums.length;
long total = 0;
for (int x : nums) total += x;
long[] pre =newlong[n];
pre[0]= nums[0];
for (int i = 1; i < n; i++) pre[i]= pre[i-1]+ nums[i];
Map<Long, Integer> left =new HashMap<>(), right =new HashMap<>();
for (int i = 0; i < n-1; i++) right.put(pre[i], right.getOrDefault(pre[i], 0) + 1);
int ans = right.getOrDefault(total/2, 0);
for (int i = 0; i < n; i++) {
long diff = k - nums[i];
long newTotal = total + diff;
if (newTotal % 2 != 0) {
if (i < n-1) {
right.put(pre[i], right.get(pre[i]) - 1);
left.put(pre[i], left.getOrDefault(pre[i], 0) + 1);
}
continue;
}
long half = newTotal / 2;
int cnt = 0;
cnt += right.getOrDefault(half - diff, 0);
cnt += left.getOrDefault(half, 0);
ans = Math.max(ans, cnt);
if (i < n-1) {
right.put(pre[i], right.get(pre[i]) - 1);
left.put(pre[i], left.getOrDefault(pre[i], 0) + 1);
}
}
return ans;
}
}
classSolution {
funwaysToPartition(nums: IntArray, k: Int): Int {
val n = nums.size
val total = nums.sum().toLong()
val pre = LongArray(n)
pre[0] = nums[0].toLong()
for (i in1 until n) pre[i] = pre[i-1] + nums[i]
val left = mutableMapOf<Long, Int>()
val right = mutableMapOf<Long, Int>()
for (i in0 until n-1) right[pre[i]] = right.getOrDefault(pre[i], 0) + 1var ans = right.getOrDefault(total/2, 0)
for (i in0 until n) {
val diff = k - nums[i]
val newTotal = total + diff
if (newTotal % 2L!=0L) {
if (i < n-1) {
right[pre[i]] = right[pre[i]]!! - 1 left[pre[i]] = left.getOrDefault(pre[i], 0) + 1 }
continue }
val half = newTotal / 2var cnt = 0 cnt += right.getOrDefault(half - diff, 0)
cnt += left.getOrDefault(half, 0)
ans = maxOf(ans, cnt)
if (i < n-1) {
right[pre[i]] = right[pre[i]]!! - 1 left[pre[i]] = left.getOrDefault(pre[i], 0) + 1 }
}
return ans
}
}
classSolution:
defwaysToPartition(self, nums: list[int], k: int) -> int:
n = len(nums)
total = sum(nums)
pre = [0] * n
pre[0] = nums[0]
for i in range(1, n):
pre[i] = pre[i-1] + nums[i]
from collections import Counter
left = Counter()
right = Counter(pre[:n-1])
ans = right[total //2] if total %2==0else0for i in range(n):
diff = k - nums[i]
new_total = total + diff
if new_total %2!=0:
if i < n-1:
right[pre[i]] -=1 left[pre[i]] +=1continue half = new_total //2 cnt = right[half - diff] + left[half]
ans = max(ans, cnt)
if i < n-1:
right[pre[i]] -=1 left[pre[i]] +=1return ans
impl Solution {
pubfnways_to_partition(nums: Vec<i32>, k: i32) -> i32 {
use std::collections::HashMap;
let n = nums.len();
let total: i64= nums.iter().map(|&x| x asi64).sum();
letmut pre =vec![0i64; n];
pre[0] = nums[0] asi64;
for i in1..n {
pre[i] = pre[i-1] + nums[i] asi64;
}
letmut left = HashMap::new();
letmut right = HashMap::new();
for i in0..n-1 {
*right.entry(pre[i]).or_insert(0) +=1;
}
letmut ans =if total %2==0 { *right.get(&(total/2)).unwrap_or(&0) } else { 0 };
for i in0..n {
let diff = k asi64- nums[i] asi64;
let new_total = total + diff;
if new_total %2!=0 {
if i < n-1 {
*right.entry(pre[i]).or_insert(0) -=1;
*left.entry(pre[i]).or_insert(0) +=1;
}
continue;
}
let half = new_total /2;
let cnt = right.get(&(half - diff)).unwrap_or(&0) + left.get(&half).unwrap_or(&0);
ans = ans.max(*cnt);
if i < n-1 {
*right.entry(pre[i]).or_insert(0) -=1;
*left.entry(pre[i]).or_insert(0) +=1;
}
}
ans asi32 }
}