Split Array Largest Sum

hard

You are given an integer array nums and an integer k.

Split nums into k non-empty subarrays — contiguous blocks that together cover the whole array, in their original order. Every split is judged by a single number: the largest subarray sum it produces.

Return the smallest possible value of that largest sum — in other words, choose the split that minimizes the maximum subarray sum.

A subarray is a contiguous part of the array.

Hints

The answer is a single number, not a split. What is the smallest value it could possibly be? What is the largest?
Fix a candidate cap and ask a yes/no question: can the array be split into at most k pieces where no piece's sum exceeds the cap? One greedy left-to-right pass answers it.
That yes/no answer flips from no to yes exactly once as the cap grows — feasibility is monotone. Binary search the cap between max(nums) and sum(nums).

Common doubts

Every element must live inside some subarray, so whichever piece contains the largest element already has a sum of at least max(nums). No split can score below that.
With non-negative values, packing each piece as full as the cap allows never hurts — closing a piece early only forces elements into later pieces sooner. So the greedy count is a true minimum, and comparing it against k decides feasibility.
If the greedy pass fits everything in fewer than k pieces, you can always cut some piece into smaller ones — sums only shrink when values are non-negative. So any cap feasible with fewer pieces is also feasible with exactly k.
No. Feasibility is monotone: infeasible caps form a prefix of the range and feasible caps a suffix. The lower-bound loop converges exactly on the first feasible cap, which observation-by-observation is the minimized largest sum.

Interview follow-ups

Both pillars collapse: growing a piece can shrink its sum, so the greedy pass no longer computes the fewest pieces, and feasibility stops being monotone in the cap. Fall back to the partition DP, which never relies on either property.
It becomes multiway partitioning — scheduling jobs on k machines to minimize the busiest machine (makespan). That version is NP-hard; you would discuss greedy heuristics like sorting descending plus best-fit, or exhaustive search with pruning for small n.
Yes — after the binary search settles on the final cap, run the greedy pass once more with that cap and record the index where each piece closes. Those boundaries reconstruct an optimal split.

Fun facts

  • This problem is a disguise of the classic painter's partition problem: painters must paint a row of fence boards in order, and you minimize the time the busiest painter works.
  • The binary-search-on-the-answer skeleton here is reusable almost verbatim: Koko Eating Bananas, Capacity to Ship Packages Within D Days, and Minimum Number of Days to Make m Bouquets all swap in a different one-pass checker under the same loop.

Asked at

GoogleAmazonMetaMicrosoftApple
Frequently Sometimes Occasionally
Example 1
Input: nums = [7,2,5,10,8], k = 2
Output: 18
Splitting into [7,2,5] and [10,8] gives sums 14 and 18, so the largest sum is 18. No other split into two subarrays does better.
Example 2
Input: nums = [1,2,3,4,5], k = 2
Output: 9
Splitting into [1,2,3] and [4,5] gives sums 6 and 9. Every other split forces a larger maximum.
Constraints

- 1 <= nums.length <= 1000 - 0 <= nums[i] <= 10^6 - 1 <= k <= min(50, nums.length)

Solve this problem →