29. Split Array Largest Sum
โ GFG solution to the Split Array Largest Sum problem: divide array into k subarrays minimizing maximum sum using binary search and greedy approach. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given an array arr[]
and an integer k
, divide the array into k contiguous subarrays such that the maximum sum among these subarrays is minimized. Find this minimum possible maximum sum.
The goal is to split the array optimally to minimize the largest subarray sum across all possible k-way splits.
๐ Examples
Example 1
Input: arr[] = [1, 2, 3, 4], k = 3
Output: 4
Explanation: Optimal Split is [1, 2], [3], [4].
Maximum sum of all subarrays is 4, which is minimum possible for 3 splits.
Example 2
Input: arr[] = [1, 1, 2], k = 2
Output: 2
Explanation: Splitting the array as [1, 1] and [2] is optimal.
This results in a maximum sum subarray of 2.
๐ Constraints
$1 \le k \le \text{arr.size()} \le 10^5$
$1 \le \text{arr}[i] \le 10^4$
โ
My Approach
The optimal approach uses Binary Search on Answer combined with a Greedy Validation function:
Binary Search + Greedy Validation
Define Search Space:
Lower bound: Maximum element in array (minimum possible answer)
Upper bound: Sum of all elements (maximum possible answer when k=1)
Binary Search on Answer:
For each candidate maximum sum
mid
, check if it's possible to split array into โค k subarraysIf possible, try for a smaller maximum sum (move right boundary left)
If not possible, increase the maximum sum (move left boundary right)
Greedy Validation:
Try to fit elements into current subarray while sum โค
mid
When adding next element would exceed
mid
, start a new subarrayCount total subarrays needed and check if โค k
Monotonic Property:
If we can split with maximum sum X, we can also split with any sum > X
This monotonic property enables binary search
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(n * log(sum)), where n is the array size and sum is the total sum of array elements. Binary search runs in O(log(sum)) iterations, and each validation takes O(n) time.
Expected Auxiliary Space Complexity: O(1), as we only use constant extra space for variables and pointers, without any additional data structures.
๐งโ๐ป Code (C++)
class Solution {
public:
int splitArray(vector<int>& a, int k) {
int l = *max_element(a.begin(), a.end()), r = accumulate(a.begin(), a.end(), 0);
while (l < r) {
int m = (l + r) / 2, s = 0, c = 1;
for (int x : a) {
if (s + x > m) c++, s = 0;
s += x;
}
if (c <= k) r = m;
else l = m + 1;
}
return l;
}
};
๐งโ๐ป Code (Java)
class Solution {
public int splitArray(int[] a, int k) {
int l = 0, r = 0;
for (int x : a) {
l = Math.max(l, x);
r += x;
}
while (l < r) {
int m = (l + r) / 2, s = 0, c = 1;
for (int x : a) {
if (s + x > m) {
c++;
s = 0;
}
s += x;
}
if (c <= k) r = m;
else l = m + 1;
}
return l;
}
}
๐ Code (Python)
class Solution:
def splitArray(self, a, k):
l, r = max(a), sum(a)
while l < r:
m, s, c = (l + r) // 2, 0, 1
for x in a:
if s + x > m:
c += 1
s = 0
s += x
if c <= k: r = m
else: l = m + 1
return l
๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated