LeetCode #410 Hard

Split Array Largest Sum

Split Array Largest Sum is LeetCode 410 (Hard). You get an integer array nums and an integer k. Cut nums into exactly k non-empty pieces, each one a contiguous subarray, and look at the biggest piece sum. Return the smallest value that biggest sum can have.

  • Numbers are between 0 and 10⁶, and there are at most 1,000 of them.
  • k is at most 50 and never more than nums.length, so every input has a valid split and there is no "impossible" answer to return.

The total is at most 10⁹, which fits in a 32-bit int.

Constraints
  • 1 <= nums.length <= 1000
  • 0 <= nums[i] <= 10⁶
  • 1 <= k <= min(50, nums.length)
binary-searcharraybinary-search-on-answergreedy
Open on LeetCode ↗
02

Intuition

Choosing where the k - 1 cuts go is hard. Testing a fixed limit is easy. Say no piece may sum to more than cap. Walk left to right, keep adding numbers to the current piece, and cut only when the next number would push it past cap. That gives the fewest pieces any split can have under cap.

  • One direction: a bigger cap never needs more pieces, so the limits that need at most k pieces form one unbroken run.
  • Where to look: the answer is at least max(nums), since some piece holds the largest number, and at most sum(nums), a single piece.

Binary search that range for the smallest limit that works.

How to spot this pattern

"Cut into k contiguous parts and make the worst part as small as possible" is a minimise-the-maximum question. Guess the maximum, count how many parts that guess forces, and binary search the guess.

03

Approach

Try it first

Before reading on, pick a limit such as 20 for [7, 2, 5, 10, 8] and count how many pieces it forces. Then ask how that count changes as the limit grows. Aim for O(n log S), where S is the sum of the array.

1

Set the search range

Set lo = max(nums) and hi = sum(nums). A number cannot be split across pieces, so no limit below the largest one can work, and the full sum always works because one piece can take everything.

2

Count the pieces a limit forces

Start with one piece and a running sum of 0. For each number, if adding it would pass cap, start a new piece holding just that number; otherwise add it. Cutting only when forced keeps every piece as long as possible, so the count is the smallest one possible.

3

Halve the range

While lo < hi, try mid = (lo + hi) // 2. If it needs at most k pieces, set hi = mid, because mid works and something smaller might too. Otherwise set lo = mid + 1, because mid and every limit below it need too many pieces.

4

Why fewer than k pieces still counts as a yes

A limit that needs only 2 pieces when k = 3 is still a valid answer: cut any piece again and no sum grows. The promise k <= nums.length is what makes this safe: there are always enough numbers to make the extra cuts.

5

Return the meeting point

When lo == hi, that value works and every smaller value was ruled out, so return it.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def splitArray(self, nums, k):
▶3 def pieces_needed(cap):
▶4 pieces, run = 1, 0
▶5 for x in nums:
▶6 if run + x > cap:
▶7 pieces += 1
▶8 run = x
▶9 else:
▶10 run += x
▶11 return pieces
▶12 
▶13 lo, hi = max(nums), sum(nums)
▶14 while lo < hi:
▶15 mid = (lo + hi) // 2
▶16 if pieces_needed(mid) <= k:
▶17 hi = mid
▶18 else:
▶19 lo = mid + 1
▶20 return lo
nums725108limits triednone yetanswer range10 to 32piecesno cap tried yetthe answer is between 10 and 32
lo10largest number
hi32whole sum
k2
Some piece has to hold the 10, so no answer can be below it. One piece holding everything sums to 32, so no answer needs to be above it. The search looks for the smallest limit in this range that needs at most 2 pieces.
nums725108limits tried21answer range10 to 32piecescap 21piece 17try cap 21: 7 opens piece 1
cap21
piece 17of 21
pieces1
The range is 10 to 32, so try the middle, 21. The first number always opens piece 1.
nums725108limits tried21answer range10 to 32piecescap 21piece 1722 fits → piece 1 sums to 9
cap21
piece 19of 21
pieces1
Piece 1 holds 7 and 2 more makes 9, still within 21. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried21answer range10 to 32piecescap 21piece 17255 fits → piece 1 sums to 14
cap21
piece 114of 21
pieces1
Piece 1 holds 9 and 5 more makes 14, still within 21. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried21answer range10 to 32piecescap 21piece 172510piece 21010 would make 24, over 21 → piece 2 starts
cap21
piece 210of 21
pieces2
Piece 1 holds 14. Adding 10 would make 24, more than the cap of 21, so cut here and start piece 2 with 10.
nums725108limits tried21answer range10 to 21piecescap 21piece 1725piece 21088 fits → 2 pieces: cap 21 works
cap21
piece 218of 21
pieces2
range10–21
Piece 2 holds 10 and 8 more makes 18, still within 21. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible. That is 2 pieces, at most 2, so cap 21 works. A smaller cap might work too, so the range shrinks to 10 to 21.
nums725108limits tried2115answer range10 to 21piecescap 15piece 17try cap 15: 7 opens piece 1
cap15
piece 17of 15
pieces1
The range is 10 to 21, so try the middle, 15. The first number always opens piece 1.
nums725108limits tried2115answer range10 to 21piecescap 15piece 1722 fits → piece 1 sums to 9
cap15
piece 19of 15
pieces1
Piece 1 holds 7 and 2 more makes 9, still within 15. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried2115answer range10 to 21piecescap 15piece 17255 fits → piece 1 sums to 14
cap15
piece 114of 15
pieces1
Piece 1 holds 9 and 5 more makes 14, still within 15. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried2115answer range10 to 21piecescap 15piece 172510piece 21010 would make 24, over 15 → piece 2 starts
cap15
piece 210of 15
pieces2
Piece 1 holds 14. Adding 10 would make 24, more than the cap of 15, so cut here and start piece 2 with 10.
nums725108limits tried2115answer range16 to 21piecescap 15piece 1725piece 2108piece 388 starts piece 3 → 3 pieces: cap 15 too small
cap15
piece 38of 15
pieces3
range16–21
Piece 2 holds 10. Adding 8 would make 18, more than the cap of 15, so cut here and start piece 3 with 8. That is 3 pieces, more than 2, so cap 15 is too small, and so is every cap below it. The range moves up to 16 to 21.
nums725108limits tried211518answer range16 to 21piecescap 18piece 17try cap 18: 7 opens piece 1
cap18
piece 17of 18
pieces1
The range is 16 to 21, so try the middle, 18. The first number always opens piece 1.
nums725108limits tried211518answer range16 to 21piecescap 18piece 1722 fits → piece 1 sums to 9
cap18
piece 19of 18
pieces1
Piece 1 holds 7 and 2 more makes 9, still within 18. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried211518answer range16 to 21piecescap 18piece 17255 fits → piece 1 sums to 14
cap18
piece 114of 18
pieces1
Piece 1 holds 9 and 5 more makes 14, still within 18. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried211518answer range16 to 21piecescap 18piece 172510piece 21010 would make 24, over 18 → piece 2 starts
cap18
piece 210of 18
pieces2
Piece 1 holds 14. Adding 10 would make 24, more than the cap of 18, so cut here and start piece 2 with 10.
nums725108limits tried211518answer range16 to 18piecescap 18piece 1725piece 21088 fits → 2 pieces: cap 18 works
cap18
piece 218of 18
pieces2
range16–18
Piece 2 holds 10 and 8 more makes 18, still within 18. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible. That is 2 pieces, at most 2, so cap 18 works. A smaller cap might work too, so the range shrinks to 16 to 18.
nums725108limits tried21151817answer range16 to 18piecescap 17piece 17try cap 17: 7 opens piece 1
cap17
piece 17of 17
pieces1
The range is 16 to 18, so try the middle, 17. The first number always opens piece 1.
nums725108limits tried21151817answer range16 to 18piecescap 17piece 1722 fits → piece 1 sums to 9
cap17
piece 19of 17
pieces1
Piece 1 holds 7 and 2 more makes 9, still within 17. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried21151817answer range16 to 18piecescap 17piece 17255 fits → piece 1 sums to 14
cap17
piece 114of 17
pieces1
Piece 1 holds 9 and 5 more makes 14, still within 17. Keep it in the same piece: cutting only when forced keeps the piece count as low as possible.
nums725108limits tried21151817answer range16 to 18piecescap 17piece 172510piece 21010 would make 24, over 17 → piece 2 starts
cap17
piece 210of 17
pieces2
Piece 1 holds 14. Adding 10 would make 24, more than the cap of 17, so cut here and start piece 2 with 10.
nums725108limits tried21151817answer range18piecescap 17piece 1725piece 2108piece 388 starts piece 3 → 3 pieces: cap 17 too small
cap17
piece 38of 17
pieces3
range18
Piece 2 holds 10. Adding 8 would make 18, more than the cap of 17, so cut here and start piece 3 with 8. That is 3 pieces, more than 2, so cap 17 is too small, and so is every cap below it. The range moves up to 18.
nums725108limits tried21151817answer range18piecescap 18piece 1725piece 2108range closed → answer 18
answer18
piece sums14, 18
The range has closed on 18: every smaller cap needs more than 2 pieces, and 18 does not. Cutting at this cap gives piece sums 14, 18, and the biggest is exactly 18.
05

DP over prefixes

prev[p] is the best biggest sum when the first p numbers are cut into j - 1 pieces. For the first i numbers in j pieces, the last piece is nums[p:i], so the cost of that choice is max(prev[p], sum of nums[p:i]). Walking p from right to left makes the last piece grow, so once it alone reaches the best value found, no smaller p can help and the loop stops.

▶1class Solution:
▶2 def splitArray(self, nums, k):
▶3 n = len(nums)
▶4 prefix = [0] * (n + 1)
▶5 for i, x in enumerate(nums):
▶6 prefix[i + 1] = prefix[i] + x
▶7 
▶8 INF = float("inf")
▶9 prev = [0] + [INF] * n
▶10 for j in range(1, k + 1):
▶11 cur = [INF] * (n + 1)
▶12 for i in range(j, n + 1):
▶13 best = INF
▶14 for p in range(i - 1, j - 2, -1):
▶15 last = prefix[i] - prefix[p]
▶16 if last >= best:
▶17 break
▶18 best = min(best, max(prev[p], last))
▶19 cur[i] = best
▶20 prev = cur
▶21 return prev[n]
06

Common pitfalls

Starting the search below the largest number

✗ Wrong
lo, hi = 0, sum(nums)
✓ Right
lo, hi = max(nums), sum(nums)

When a number is bigger than cap, the count still opens a piece for it and carries on, so a limit that no split can meet gets reported as fine. On [3, 9] with k = 2 the search returns 3, but the piece holding 9 sums to 9.

Testing for exactly k pieces

✗ Wrong
if pieces_needed(mid) == k:
    hi = mid
✓ Right
if pieces_needed(mid) <= k:
    hi = mid

== k rejects every limit that needs fewer pieces, even though such limits work, so the search drifts upward. On [1, 4, 4] with k = 3, limits 4 to 8 need only 2 or 3 pieces, and the search returns 9 instead of 4.

Counting pieces from 0

✗ Wrong
pieces, run = 0, 0
✓ Right
pieces, run = 1, 0

The first piece exists before any cut is made. Starting at 0 reports one piece fewer than the truth, so limits that need k + 1 pieces pass. On [7, 2, 5, 10, 8] with k = 2 the search returns 14 instead of 18.

07

Edge cases

Every number is 0

max(nums) and sum(nums) are both 0, so the loop never runs and the answer is 0. Zeros elsewhere are harmless: adding 0 never pushes a piece past the limit.

k equals the length of nums

Each number sits alone, so the answer is max(nums). Every limit from there up needs at most k pieces, so the search closes on lo.

08

Complexity

Time
O(n log S)
Space
O(1)
n is the length of nums and S is sum(nums) - max(nums). S is at most 10⁹, so the loop runs about 30 times, each with one O(n) counting pass. Only a few integers are stored.
09

Split Array Largest Sum FAQ

Is this the same as Allocate Minimum Pages?

The task is the same: contiguous groups, smallest possible largest group. Two details differ. Allocate Minimum Pages allows more students than books and expects -1 then, while here k <= nums.length is promised, so there is always an answer. Its inputs also reach 10⁶ books of up to 10⁴ pages, so totals need 64-bit integers; here the total stays within 10⁹.

Why is the answer always the sum of some actual piece?

Let C be the value the search returns and run the greedy cut with limit C. Its largest piece sum M is at most C, and those same cuts prove that M works. C is the smallest value that works, so M cannot be below it: M = C. For [7, 2, 5, 10, 8] the answer 18 is the piece [10, 8].

What if the array could contain negative numbers?

Binary search on the limit breaks. Adding a number can now shrink a piece's sum, so cutting only when forced is no longer the best plan, and max(nums) is no longer a lower bound. The DP version still works, because it tries every start for the last piece instead of relying on sums only growing.