Split Array Largest Sum - Problem

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.

Input & Output

Example 1 — Basic Split
$ Input: nums = [7,2,5,10,8], k = 2
Output: 18
💡 Note: Split into [7,2,5] and [10,8]. The sums are 14 and 18, so the largest sum is 18, which is minimal among all possible splits.
Example 2 — Multiple Small Subarrays
$ Input: nums = [1,2,3,4,5], k = 2
Output: 9
💡 Note: Split into [1,2,3,4] and [5]. The sums are 10 and 5, giving largest sum 10. Better split: [1,2,3] and [4,5] gives sums 6 and 9, so answer is 9.
Example 3 — Edge Case k=1
$ Input: nums = [1,4,4], k = 1
Output: 9
💡 Note: Cannot split array when k=1, so return sum of entire array: 1+4+4=9.

Constraints

  • 1 ≤ nums.length ≤ 1000
  • 1 ≤ k ≤ min(50, nums.length)
  • 0 ≤ nums[i] ≤ 106

Visualization

Tap to expand
Split Array Largest Sum INPUT Array nums: 7 2 5 10 8 [0] [1] [2] [3] [4] k = 2 (splits) Total sum = 32 Search range: min=max(nums)=10 max=sum(nums)=32 Goal: Find minimum possible largest subarray sum GREEDY + BINARY SEARCH 1 Binary Search Setup lo=10, hi=32, mid=21 2 Greedy Check Can split with max=mid? 3 Count Subarrays Greedily count splits needed 4 Adjust Range splits <= k? hi=mid : lo=mid+1 Binary Search Iterations: mid=21: [7,2,5]=14, [10,8]=18 splits=2 <= k, hi=21 mid=15: [7,2,5]=14, [10]=10 [8] splits=3 > k, lo=16 mid=18: [7,2,5]=14, [10,8]=18 splits=2 <= k, hi=18 lo=18, hi=18 --> Answer: 18 FINAL RESULT Optimal Split: [7,2,5] sum = 14 [10,8] sum = 18 Output: 18 Maximum subarray sum is minimized to 18 Verification: - Split into k=2 subarrays: OK - max(14, 18) = 18 - No split gives smaller max Key Insight: Binary search on the answer space [max(nums), sum(nums)]. For each candidate maximum sum, greedily check if array can be split into at most k subarrays. This transforms minimization problem into a feasibility check. Time: O(n log(sum)), Space: O(1). TutorialsPoint - Split Array Largest Sum | Greedy + Binary Search Approach
Asked in
Google 45 Amazon 38 Facebook 32 Microsoft 28
120.0K Views
High Frequency
~25 min Avg. Time
2.7K Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen