◀ THE GRIND — BINARY SEARCH

Split Array Largest Sum

The drill: Cut an array into k contiguous, non-empty pieces so the heaviest piece is as light as possible — return that minimized largest-piece sum across the best split.

THE BRIEFING — THE FULL DRILL, IN MY OWN WORDS

An array of non-negative integers needs to be cut into exactly k contiguous, non-empty pieces — no skipping elements, no piece left empty, and the original order of elements never changes.

Every possible way of making those k cuts produces some set of piece sums, and each split has a worst piece — the single largest sum among its pieces.

The job is to choose the split that makes that worst piece as small as possible, and report the sum of that piece under the best possible split.

EX 01
nums = [3, 1, 4, 1, 5, 9, 2, 6] · k = 3
14
BEST SPLIT IS [3,1,4,1,5]=14, [9,2]=11, [6]=6
EX 02
nums = [2, 4, 6, 8] · k = 1
20
K=1, THE WHOLE ARRAY IS THE ONLY PIECE
EX 03
nums = [5, 1, 9, 2] · k = 4
9
K=N, EVERY ELEMENT IS ITS OWN PIECE
THE HINTS — TAKE ONLY WHAT YOU NEED
HINT 1 THE NUDGE

A DP over “first i elements split into j pieces” finds the exact minimum, but it explores every cut point. What if, instead of building the split, you guessed its answer and only checked whether that guess works?

HINT 2 THE STRUCTURE

For a candidate cap M, greedily pack elements left to right until adding one would overflow M, then start a new piece — that greedy count is the fewest pieces possible under M, and it only grows as M shrinks.

HINT 3 ONE STEP FROM THE ANSWER

Binary-search M between the largest single element and the total sum: if the greedy piece count for M fits within k, M is big enough — shrink the search; otherwise M is too small — grow it.

COACH'S BOARD — THE PATTERN, STEP BY STEP
THE FAIREST SPLITPATTERN · BINARY SEARCH THE ANSWERnums = [3, 1, 4, 1, 5, 9, 2, 6] · k = 3
3
1
4
1
5
9
2
6
BINARY SEARCH ON THE CAP
— empty —
STEP 1

8 elements split into k=3 contiguous pieces. Binary-search the cap on the largest piece, between 9 (the max element) and 31 (the total).

STEP 1 / 7 · ← → WORK TOO
THE SPLITS — TWO PACES, TWO LANGUAGES
grind/split-array-largest-sum.pyRACE PACE
LANG ▸
PACE ▸
class Solution:
    def splitArray(self, nums: List[int], k: int) -> int:
        def pieces_needed(cap: int) -> int:
            pieces, current = 1, 0
            for v in nums:
                if current + v > cap:
                    pieces += 1
                    current = v
                else:
                    current += v
            return pieces

        lo, hi = max(nums), sum(nums)
        while lo < hi:
            mid = (lo + hi) // 2
            if pieces_needed(mid) <= k:
                hi = mid
            else:
                lo = mid + 1
        return lo
TIME O(N·LOG(SUM−MAX))SPACE O(1)PYTHON · RACE PACE · 20 LN

✓ CHIP-TIMED — ALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED