Watch 10 video solutions for Split Array Largest Sum, a hard level problem involving Array, Binary Search, Dynamic Programming. This walkthrough by NeetCode has 125,415 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given an integer array nums and an integer k, split nums into k non-empty subarrays such that the largest sum of any subarray is minimized.
Return the minimized largest sum of the split.
A subarray is a contiguous part of the array.
Example 1:
Input: nums = [7,2,5,10,8], k = 2 Output: 18 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
Example 2:
Input: nums = [1,2,3,4,5], k = 2 Output: 9 Explanation: There are four ways to split nums into two subarrays. The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
Constraints:
1 <= nums.length <= 10000 <= nums[i] <= 1061 <= k <= min(50, nums.length)Problem Overview: Given an integer array nums and an integer k, split the array into k non-empty contiguous subarrays. The goal is to minimize the largest sum among these subarrays. You must decide where to split so that the maximum subarray sum is as small as possible.
Approach 1: Dynamic Programming with Prefix Sum (O(n² * k) time, O(n * k) space)
This method builds the solution by considering every possible split position. Use a prefix sum array to compute subarray sums in O(1). Let dp[i][j] represent the minimum possible largest subarray sum when splitting the first i elements into j parts. For each state, iterate over all previous split points p and minimize max(dp[p][j-1], sum(p+1..i)). The prefix sum array avoids recomputing sums repeatedly. This approach guarantees the optimal answer but becomes expensive for large n because every state checks multiple partitions.
Approach 2: Binary Search with Greedy Split (O(n log(sum(nums))) time, O(1) space)
The key observation: the answer lies between max(nums) and sum(nums). Instead of testing every partition, perform binary search on this range. For a candidate maximum sum mid, scan the array and greedily form subarrays while the running sum stays ≤ mid. When the sum exceeds mid, start a new subarray. This greedy check runs in O(n) and tells you how many subarrays are required. If more than k subarrays are needed, the limit is too small; increase the search range. Otherwise, try a smaller maximum. This technique combines greedy validation with binary search to efficiently converge on the optimal value.
Recommended for interviews: The binary search + greedy solution is what most interviewers expect. It reduces the search space dramatically and runs in O(n log(sum)). The dynamic programming approach shows strong understanding of partition DP and is useful for reasoning about the problem, but the optimized binary search approach demonstrates algorithmic insight and scalability.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Dynamic Programming with Prefix Sum | O(n² * k) | O(n * k) | Useful for understanding partition DP or when constraints are small |
| Binary Search with Greedy Split | O(n log(sum(nums))) | O(1) | Optimal solution for large arrays and the expected interview approach |