Split Array Largest Sum (LeetCode 410): Solution and Explanation
Split an array of non-negative integers into a given number of non-empty contiguous pieces so that the largest piece sum is as small as possible. Return that smallest possible largest sum.
Watch it run step by step
Try your own input and write the code →Intuition
The textbook approach is dynamic programming: let dp[j][i] be the best largest sum when splitting the first i elements into j pieces, and try every position for the last cut. That is O(m × n²), which passes for arrays of 1,000 elements but is heavy, and it hides a much simpler structure.
Turn the optimization question into a yes or no question. Instead of asking for the best split, ask whether you can split the array into at most m pieces so that no piece exceeds some limit L. That check is easy and greedy: walk left to right, keep adding elements to the current piece, and start a new piece only when adding the next element would push the sum over L. Packing each piece as full as possible uses the fewest pieces.
The yes or no answer is monotonic in L. If a limit works, any larger limit works too. So the smallest workable limit can be found with binary search between two natural bounds. The limit can never be below the largest single element, since that element has to live in some piece, and it never needs to exceed the total sum, which corresponds to one piece holding everything.
Why is at most m pieces enough when the problem asks for exactly m? Because if the greedy uses fewer pieces, you can split any piece with more than one element further without increasing the largest sum, so reaching exactly m is always possible when the array has at least m elements.
Approach: Binary search on the answer with greedy check
- Define
canSplit(largest): walk the array adding each value tocurSum, and whencurSum > largest, count a cut and start the new piece at the current value. canSplitreturns whether the number of pieces,subarray + 1, is at most m.- Set
l = max(nums)andr = sum(nums), andres = r. - While
l <= r, testmid = l + (r - l) // 2. - If
canSplit(mid)is true, recordres = midand try smaller limits withr = mid - 1. - Otherwise try larger limits with
l = mid + 1, and returnresat the end.
Step-by-step example
nums = [7,2,5,10,8], m = 2
- The range is 10 to 32. Limit 21 makes pieces [7,2,5] (14) and [10,8] (18), which is 2 pieces. It works, so
res= 21 andr= 20. - Limit 15 makes [7,2,5], [10] and [8], which is 3 pieces. It fails, so
l= 16. - Limit 18 makes [7,2,5] and [10,8], since 10 + 8 = 18 does not exceed 18. That is 2 pieces, so
res= 18 andr= 17. - Limit 16 and then limit 17 both need 3 pieces because 10 + 8 exceeds them. After those,
l= 18 andr= 17. - The search ends and returns 18, from the split [7,2,5] and [10,8].
Code: Python, Java, C++
1class Solution:
2 def splitArray(self, nums: List[int], m: int) -> int:
3 def canSplit(largest):
4 subarray = 0
5 curSum = 0
6 for n in nums:
7 curSum += n
8 if curSum > largest:
9 subarray += 1
10 curSum = n
11 return subarray + 1 <= m
12
13 l, r = max(nums), sum(nums)
14 res = r
15
16 while l <= r:
17 mid = l + ((r - l) // 2)
18 if canSplit(mid):
19 res = mid
20 r = mid - 1
21 else:
22 l = mid + 1
23 return res1class Solution {
2 public int splitArray(int[] nums, int m) {
3 int l = 0, r = 0;
4 for (int n : nums) {
5 l = Math.max(l, n);
6 r += n;
7 }
8 int res = r;
9
10 while (l <= r) {
11 int mid = l + (r - l) / 2;
12 if (canSplit(mid, nums, m)) {
13 res = mid;
14 r = mid - 1;
15 } else {
16 l = mid + 1;
17 }
18 }
19 return res;
20 }
21
22 private boolean canSplit(int largest, int[] nums, int m) {
23 int subarray = 1;
24 int curSum = 0;
25 for (int n : nums) {
26 curSum += n;
27 if (curSum > largest) {
28 subarray++;
29 curSum = n;
30 }
31 }
32 return subarray <= m;
33 }
34}1class Solution {
2public:
3 int splitArray(vector<int>& nums, int m) {
4 int l = 0, r = 0;
5 for (int n : nums) {
6 l = max(l, n);
7 r += n;
8 }
9 int res = r;
10
11 while (l <= r) {
12 int mid = l + (r - l) / 2;
13 if (canSplit(mid, nums, m)) {
14 res = mid;
15 r = mid - 1;
16 } else {
17 l = mid + 1;
18 }
19 }
20 return res;
21 }
22
23 bool canSplit(int largest, vector<int>& nums, int m) {
24 int subarray = 1;
25 int curSum = 0;
26 for (int n : nums) {
27 curSum += n;
28 if (curSum > largest) {
29 subarray++;
30 curSum = n;
31 }
32 }
33 return subarray <= m;
34 }
35};Time and space complexity
O(n log S)
S is the sum of the array. The binary search runs O(log S) times over the range from the maximum element to S, and each greedy check is O(n).
O(1)
The check and the search use a fixed number of variables.
Other approaches
Dynamic programming with prefix sums (time O(m × n²), space O(m × n))
Let dp[j][i] be the minimum largest sum for the first i elements in j pieces, computed by trying every last cut with prefix sums. It is the general approach and works for variants where the greedy check does not apply, but it is much slower.
Top-down memoized recursion (time O(m × n²), space O(m × n))
Recurse on (start index, pieces left), trying each end for the current piece and memoizing results. It has the same complexity as the bottom-up DP and is often easier to write first.
Common mistakes
- Resetting
curSumto 0 when starting a new piece instead of to the current value, which drops that element from the new piece. - Setting the lower bound below
max(nums). With a smaller limit, a single large element can never fit and the greedy check gives misleading counts. - Returning true only when exactly m pieces are used. Fewer pieces can always be split further, so the check should be at most m.
- Summing in a 32-bit integer in Java or C++. Here the total is at most 10^9, which just fits, but larger variants overflow, so a long is the safe habit.
Frequently asked questions
Why does binary search work for Split Array Largest Sum?
Whether you can split into at most m pieces with every piece at most L is monotonic in L. Once a limit works, every larger limit works, so binary search finds the smallest one.
Why is the greedy split check correct?
Filling each piece as much as possible before cutting uses the fewest pieces for a given limit. If the greedy needs more than m pieces, no other split can do it with m.
What is the time complexity?
It is O(n log S), where S is the total sum. That is much faster than the O(m × n²) dynamic programming approach.
Is this the same as Capacity To Ship Packages Within D Days?
Yes. The ship capacity is the largest piece sum and the number of days is the number of pieces, so the same binary search and greedy check solve both.