Maximum Number of Ways to Partition an Array - Problem

You're given a 0-indexed integer array nums of length n. Your goal is to find pivot points where you can split the array into two equal-sum parts.

A valid partition occurs at index pivot when:

  • 1 โ‰ค pivot < n (can't split at the very beginning or end)
  • Sum of left part equals sum of right part: nums[0] + ... + nums[pivot-1] == nums[pivot] + ... + nums[n-1]

Here's the twist: you can optionally change exactly one element in the array to value k before counting partitions. This gives you the power to potentially create more valid pivot points!

Return the maximum possible number of ways to partition the array after making at most one strategic change.

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [2, -1, 2], k = 3
โ€บ Output: 1
๐Ÿ’ก Note: Original array has 0 valid partitions. If we change nums[2] to 3: [2, -1, 3], pivot at index 2 gives left_sum = 1, right_sum = 3 (not equal). If we change nums[1] to 3: [2, 3, 2], pivot at index 2 gives left_sum = 5, right_sum = 2 (not equal). However, if we change nums[0] to 3: [3, -1, 2], pivot at index 2 gives left_sum = 2, right_sum = 2 (equal!). Maximum is 1.
example_2.py โ€” No Change Optimal
$ Input: nums = [0, 1, -1, 0], k = 2
โ€บ Output: 2
๐Ÿ’ก Note: Original array already has valid partitions at indices 1 and 2. At pivot 1: left=[0], right=[1,-1,0], sums are 0 vs 0. At pivot 2: left=[0,1], right=[-1,0], sums are 1 vs -1 (not equal). Actually at pivot 3: left=[0,1,-1], right=[0], sums are 0 vs 0. The original array gives us 2 partitions, which is optimal.
example_3.py โ€” Single Element
$ Input: nums = [1], k = 1
โ€บ Output: 0
๐Ÿ’ก Note: Arrays with length 1 cannot be partitioned since we need at least one element on each side of the pivot. No valid partitions possible.

Constraints

  • n == nums.length
  • 2 โ‰ค n โ‰ค 105
  • -106 โ‰ค nums[i] โ‰ค 106
  • -106 โ‰ค k โ‰ค 106

Visualization

Tap to expand
2-12k??PivotLeft DifferencesTrack splits wherechange affects leftRight DifferencesTrack splits wherechange affects right
Understanding the Visualization
1
Setup
Place gems on scale, calculate weight differences for each split point
2
Organize
Group differences by value - differences to the left and right of current position
3
Transform
For each position, consider transforming that gem and count balanced splits
4
Update
Move differences between left and right groups as position advances
5
Optimize
Track maximum balanced splits found across all transformation positions
Key Takeaway
๐ŸŽฏ Key Insight: Instead of recalculating everything for each change, we incrementally track how differences shift between left and right sides, using hash maps to instantly count valid partitions!
Asked in
Google 28 Amazon 15 Microsoft 12 Meta 8
26.8K Views
Medium-High Frequency
~25 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen