Problem Statement

Split Array Largest Sum

You have a list of numbers called nums and a number k. You need to cut the list into k pieces, where each piece is a chunk of numbers sitting next to each other, and no piece is empty. Every piece has a sum (you add up the numbers in it). One of those pieces will have the biggest sum. Your job is to cut the list in the smartest way so that this biggest sum is as small as possible. Return that smallest possible biggest sum.

hardBinary SearchDynamic ProgrammingBinary SearchTime: O(n log(sum)) · Space: O(1)

Signals to notice

minimize the largest sum among m subarraysbinary search on the answerfeasibility check

Brute force first

Try all ways to split the array into m contiguous subarrays. Exponential in the number of splits. That direct path helps you understand the question, but it tends to treat every possibility as brand new instead of learning from earlier steps.

The key insight

Binary search on the answer (maximum subarray sum). For each candidate, greedily check if the array can be split into ≤ m parts where each part's sum ≤ candidate. Once you hold onto the right piece of information from moment to moment, the problem feels less like trial and error and more like following a shape that was there all along.

Trace it on nums=[7,2,5,10,8], k=2

init: left=max=10, right=sum=32 (search range of candidate largest-sum)
mid=21: greedy parts [7,2,5]=14 | [10,8]=18 -> splits=2 <= 2 feasible -> right=21
mid=15: parts [7,2,5]=14 | [10]=10 | [8]=8 -> splits=3 > 2 too tight -> left=mid+1=16
mid=18: parts [7,2,5]=14 | [10,8]=18 -> splits=2 <= 2 feasible -> right=18
mid=17: parts [7,2,5]=14 | [10]=10 | [8]=8 -> splits=3 > 2 too tight -> left=mid+1=18; now left==right=18 -> exit loop
return left = 18

What must stay true

If a maximum sum of S allows splitting into ≤ m parts, any S' > S also works. This monotonicity makes binary search valid. Lower bound = max(nums), upper bound = sum(nums). When you keep that truth intact, each local choice supports the larger solution instead of fighting it.

Shape of the loop

left, right = max(nums), sum(nums)
while left < right:
    mid = (left + right) // 2
    splits = greedily count parts each with sum <= mid
    if splits <= k: right = mid     # mid feasible, try smaller
    else:           left = mid + 1  # too tight, raise floor
return left

Pseudocode only — the full worked solution lives in the Solution tab.

Easy way to go wrong

Lower bound should be max(nums), not 0 — you can't split a single element, so no subarray can have a sum smaller than the largest element. When the code becomes mechanical before the idea is clear, small edge cases start breaking the whole story.

Binary Search Pattern