Split Array Largest Sum
Expert Answer & Key Takeaways
A complete guide to understanding and implementing Two Pointers.
Split Array Largest Sum
Given an array
nums and an integer k, split the array into k non-empty contiguous subarrays such that the largest sum among these k subarrays is minimized.nums = [7, 2, 5, 10, 8], k = 2
Possible Splits:
1. [7], [2, 5, 10, 8] -> Max Sum: 25
2. [7, 2], [5, 10, 8] -> Max Sum: 23
3. [7, 2, 5], [10, 8] -> Max Sum: 18 (Optimal!)
4. [7, 2, 5, 10], [8] -> Max Sum: 24
Binary Search Range: [Max(nums)=10, Sum(nums)=32]Examples
Input: nums = [7,2,5,10,8], k = 2
Output: 18
The optimal split is [7,2,5] and [10,8], where the maximum sum of any subarray is 18.
Approach 1
Level I: Brute Force (Recursion with Memoization)
Intuition
Try every possible way to split the array into k parts and find the one that minimizes the maximum subarray sum. This can be done using recursion with memoization.
⏱ 💾 O(N * k)
Detailed Dry Run
nums=[7,2,5], k=2.
Split at [7], [2,5] -> max(7, 7)=7.
Split at [7,2], [5] -> max(9, 5)=9.
Min is 7.
⚠️ Common Pitfalls & Tips
O(N^2 * k) complexity might be too slow for large N but illustrates the DP transition clearly.
Approach 2
Level II: Optimal (Binary Search on Answer)
Intuition
The 'minimized largest sum' must lie between the largest single element (since each element must belong to some subarray) and the sum of all elements (if k=1). Since the possibility of achieving a sum is monotonic (if we can achieve X, we can also achieve X+1), we can binary search this range. For each candidate sum, we greedily pack elements into subarrays.
The answer must be between max(nums) and sum(nums). Use binary search on this range. For a mid value, check if we can split the array into <= k subarrays using a greedy approach.
⏱ O(N log(Sum - Max))💾 O(1)
Detailed Dry Run
nums=[1, 2, 10], k=2. Range [10, 13]. mid=11. [1, 2], [10]. count=2. Valid. r=11. mid=10. [1, 2], [10]. count=2. Valid. r=10. Result=10.
⚠️ Common Pitfalls & Tips
Be careful with high N and sums; using
long (Java/C++/Go) is necessary to avoid overflow.Course4All Technical Board
Verified ExpertSenior Software Engineers & Algorithmic Experts
Our DSA content is authored and reviewed by engineers from top tech firms to ensure optimal time and space complexity analysis.
Pattern: 2026 Ready
Updated: Weekly
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.