410. Split Array Largest Sum
Asked at Google, Amazon, Uber, Pinterest
Difficulty
Hard
Time
O(n * log(sum(nums)))
Space
O(1)
Pattern
Array
Companies
4
Problem
You are given an integer array nums and an integer k. You must cut nums into exactly k non-empty pieces. Each piece must be a contiguous block of the original array — you can only cut between neighbours, and every element must end up in exactly one piece.
For a given split, look at the sum of each piece and take the largest of those sums. Your goal is to choose the split that makes this largest sum as small as possible. Return that smallest possible largest sum.
This is a "minimize the maximum" problem. The answer is never smaller than the biggest single element (that element must live in some piece), and never bigger than the total sum of the array (one piece holding everything).
Cut into k blocks, shrink the biggest block sum
Constraints
- ●
1 <= nums.length <= 1000 - ●
0 <= nums[i] <= 10^6 - ●
1 <= k <= min(50, nums.length)
Examples
nums = [7,2,5,10,8], k = 218nums = [1,2,3,4,5], k = 29Asked At
4 companies| Company | Difficulty | |
|---|---|---|
| Hard | View all Google questions → | |
| Amazon | Hard | View all Amazon questions → |
| Uber | Hard | View all Uber questions → |
| Hard | View all Pinterest questions → |
How to Think About It
Brute force: try all possible ways to split the array into k subarrays. There are C(n-1, k-1) ways, which is exponential. Not feasible.
DP approach: dp[i][j] = minimum largest sum when splitting nums[0..i-1] into j subarrays. Transition: dp[i][j] = min over all valid p of max(dp[p][j-1], sum(p+1..i)). Time: O(n^2 * k). Works for small n but too slow for large inputs.
Binary search on the answer: the answer (minimum largest sum) is between max(nums) (each element in its own subarray) and sum(nums) (all elements in one subarray). Binary search this range. For each candidate sum, check if we can split into <= k subarrays where each subarray sum <= candidate.
Greedy check function: given a max sum limit, iterate through the array, greedily forming subarrays. Start a new subarray when adding the next element would exceed the limit. Count the number of subarrays needed. If count <= k, the candidate works (try smaller). If count > k, the candidate is too small (try larger).
Time: O(n * log(sum - max)). Space: O(1) for the greedy check.
Optimal Approach
Binary search on the answer. Set lo = max(nums), hi = sum(nums). While lo < hi: compute mid = (lo + hi) // 2. Greedy check: can we split into <= k subarrays with each sum <= mid? If yes, hi = mid (try smaller). If no, lo = mid + 1 (need larger). Return lo.
Greedy check: iterate through nums, maintain current subarray sum. If adding the next element exceeds mid, start a new subarray. Count subarrays.
Walkthrough: nums = [7, 2, 5, 10, 8], k = 2.
lo=10, hi=32, mid=21. Greedy: [7,2,5]=14, [10,8]=18. 2 <= 2.hi=21.lo=10, hi=21, mid=15. Greedy: [7,2,5]=14, [10]=10, [8]=8. 3 > 2.lo=16.lo=16, hi=21, mid=18. Greedy: [7,2,5]=14, [10,8]=18. 2 <= 2.hi=18.lo=16, hi=18, mid=17. Greedy: [7,2,5]=14, [10]=10, [8]=8. 3 > 2.lo=18.lo=hi=18. Return 18.
Time: O(n * log(sum)). Space: O(1).
Binary search the answer, check with a greedy scan
What Trips People Up in Real Interviews
Setting the binary search bounds incorrectly. lo must be max(nums) (not 0 or 1) because each subarray must contain at least one element. hi must be sum(nums) (not infinity) because the worst case is one subarray containing everything.
Off-by-one in the greedy check: starting a new subarray when current + num >= maxSum instead of > maxSum. If the current subarray sum equals the limit, it is valid. Only exceed the limit triggers a new subarray.
Confusing "minimize the largest sum" with "minimize the number of subarrays." The problem asks to minimize the maximum subarray sum given exactly k subarrays. The binary search approach finds the minimum feasible maximum.
Using int for the sum in C++/Java. The sum of the array can exceed Integer.MAX_VALUE for large arrays. Use long for the binary search bounds and the running sum in the greedy check.
Not verifying the greedy check correctly. After the loop, the count of subarrays must be <= k. If you forget to count the last subarray (it is counted at the start when count = 1), the check will be off by one.
Solution Code
O(n * log(sum(nums))) timedef splitArray(nums, k):
def canSplit(maxSum):
count, current = 1, 0
for num in nums:
if current + num > maxSum:
count += 1
current = num
else:
current += num
return count <= k
lo, hi = max(nums), sum(nums)
while lo < hi:
mid = (lo + hi) // 2
if canSplit(mid):
hi = mid
else:
lo = mid + 1
return loFrequently Asked Questions
What is the Split Array Largest Sum problem?
Given an integer array nums and an integer k, split nums into k non-empty contiguous subarrays. The largest sum of these subarrays is minimized. Return the largest sum of the split. This is a classic "minimize the maximum" binary search problem.
How do you solve Split Array Largest Sum?
The optimal approach runs in O(n * log(sum(nums))) time and O(1) space. Scroll up to "Optimal Approach" for the step-by-step walkthrough, diagrams, and code in Python, C++ and Java.
What companies ask Split Array Largest Sum?
Split Array Largest Sum is asked at Google, Amazon, Uber, Pinterest. It is a hard difficulty problem.
What are common mistakes on Split Array Largest Sum?
- Setting the binary search bounds incorrectly. `lo` must be `max(nums)` (not 0 or 1) because each subarray must contain at least one element. `hi` must be `sum(nums)` (not infinity) because the worst case is one subarray containing everything.
- Off-by-one in the greedy check: starting a new subarray when `current + num >= maxSum` instead of `> maxSum`. If the current subarray sum equals the limit, it is valid. Only exceed the limit triggers a new subarray.
- Confusing "minimize the largest sum" with "minimize the number of subarrays." The problem asks to minimize the maximum subarray sum given exactly k subarrays. The binary search approach finds the minimum feasible maximum.
- Using `int` for the sum in C++/Java. The sum of the array can exceed `Integer.MAX_VALUE` for large arrays. Use `long` for the binary search bounds and the running sum in the greedy check.
- Not verifying the greedy check correctly. After the loop, the count of subarrays must be <= k. If you forget to count the last subarray (it is counted at the start when `count = 1`), the check will be off by one.