Shubham Sourabhthe room · director's cutplain résumé
Room 04 · The Grind — Binary Search

Split Array Largest Sum

HARD✓ CHIP-TIMEDLC #410 — FULL STATEMENT

The briefingthe full drill, in my own words

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.

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.

  • array holds up to a few thousand non-negative integers
  • k is always at least one and never exceeds the array length
  • every piece must be contiguous and non-empty
  • only the minimized largest-piece sum is expected back, not the split itself
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 hintstake 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 boardthe 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 splitstwo paces, two languages

grind/split-array-largest-sum.py — RACE 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-TIMEDALL 4 SOLUTIONS RAN GREEN AGAINST SELF-AUTHORED CASES IN CI · JDK 21 · CPYTHON 3.12 · NOTHING PUBLISHES RED