Given an Array[] of N elements and a number K. ( 1 <= K <= N ) . Split the given array into K subarrays (they must cover all the elements). The maximum subarray sum achievable out of K subarrays formed, must be minimum possible. Find that possible subarray sum.
Examples:
Input : Array[] = {1, 2, 3, 4}, K = 3
Output : 4
Optimal Split is {1, 2}, {3}, {4} . Maximum sum of all subarrays is 4, which is minimum possible for 3 splits.Input : Array[] = {1, 1, 2} K = 2
Output : 2
Approach :
- Idea is to use Binary Search to find an optimal solution.
- For binary search minimum sum can be 1 and the maximum sum can be the sum of all the elements.
- To check if mid is maximum subarray sum possible. Maintain a count of sub – arrays, include all possible elements in sub array until their sum is less than mid. After this evaluation, if the count is less than or equal to K, then mid is achievable else not. (Since if the count is less than K, we can further divide any subarray its sum will never increase mid ).
- Find the minimum possible value of mid which satisfies the condition.
Below is the implementation of the above approach:
C++
// C++ implemenattion of the above approach #include <bits/stdc++.h> using namespace std; // Function to check if mid can // be maximum sub - arrays sum bool check(int mid, int array[], int n, int K) { int count = 0; int sum = 0; for (int i = 0; i < n; i++) { // If individual element is greater // maximum possible sum if (array[i] > mid) return false; // Increase sum of current sub - array sum += array[i]; // If the sum is greater than // mid increase count if (sum > mid) { count++; sum = array[i]; } } count++; // Check condition if (count <= K) return true; return false; } // Function to find maximum subarray sum // which is minimum int solve(int array[], int n, int K) { int start = 1; int end = 0; for (int i = 0; i < n; i++) { end += array[i]; } // Answer stores possible // maximum sub array sum int answer = 0; while (start <= end) { int mid = (start + end) / 2; // If mid is possible solution // Put answer = mid; if (check(mid, array, n, K)) { answer = mid; end = mid - 1; } else { start = mid + 1; } } return answer; } // Driver Code int main() { int array[] = { 1, 2, 3, 4 }; int n = sizeof(array) / sizeof(array[0]); int K = 3; cout << solve(array, n, K); } |
Java
// Java implemenattion of the above approach class GFG { // Function to check if mid can // be maximum sub - arrays sum static boolean check(int mid, int array[], int n, int K) { int count = 0; int sum = 0; for (int i = 0; i < n; i++) { // If individual element is greater // maximum possible sum if (array[i] > mid) return false; // Increase sum of current sub - array sum += array[i]; // If the sum is greater than // mid increase count if (sum > mid) { count++; sum = array[i]; } } count++; // Check condition if (count <= K) return true; return false; } // Function to find maximum subarray sum // which is minimum static int solve(int array[], int n, int K) { int start = 1; int end = 0; for (int i = 0; i < n; i++) { end += array[i]; } // Answer stores possible // maximum sub array sum int answer = 0; while (start <= end) { int mid = (start + end) / 2; // If mid is possible solution // Put answer = mid; if (check(mid, array, n, K)) { answer = mid; end = mid - 1; } else { start = mid + 1; } } return answer; } // Driver Code public static void main (String[] args) { int array[] = { 1, 2, 3, 4 }; int n = array.length ; int K = 3; System.out.println(solve(array, n, K)); } } // This code is contributed by AnkitRai01 |
Python3
# Python 3 implemenattion of the above approach # Function to check if mid can # be maximum sub - arrays sum def check(mid, array, n, K): count = 0 sum = 0 for i in range(n): # If individual element is greater # maximum possible sum if (array[i] > mid): return False # Increase sum of current sub - array sum += array[i] # If the sum is greater than # mid increase count if (sum > mid): count += 1 sum = array[i] count += 1 # Check condition if (count <= K): return True return False # Function to find maximum subarray sum # which is minimum def solve(array, n, K): start = 1 end = 0 for i in range(n): end += array[i] # Answer stores possible # maximum sub array sum answer = 0 while (start <= end): mid = (start + end) // 2 # If mid is possible solution # Put answer = mid; if (check(mid, array, n, K)): answer = mid end = mid - 1 else: start = mid + 1 return answer # Driver Code if __name__ == '__main__': array = [1, 2, 3, 4] n = len(array) K = 3 print(solve(array, n, K)) # This code is contributed by # Surendra_Gangwar |
C#
// C# implementation of the above approach using System; class GFG { // Function to check if mid can // be maximum sub - arrays sum static Boolean check(int mid, int []array, int n, int K) { int count = 0; int sum = 0; for (int i = 0; i < n; i++) { // If individual element is greater // maximum possible sum if (array[i] > mid) return false; // Increase sum of current sub - array sum += array[i]; // If the sum is greater than // mid increase count if (sum > mid) { count++; sum = array[i]; } } count++; // Check condition if (count <= K) return true; return false; } // Function to find maximum subarray sum // which is minimum static int solve(int []array, int n, int K) { int start = 1; int end = 0; for (int i = 0; i < n; i++) { end += array[i]; } // Answer stores possible // maximum sub array sum int answer = 0; while (start <= end) { int mid = (start + end) / 2; // If mid is possible solution // Put answer = mid; if (check(mid, array, n, K)) { answer = mid; end = mid - 1; } else { start = mid + 1; } } return answer; } // Driver Code public static void Main (String[] args) { int []array = { 1, 2, 3, 4 }; int n = array.Length ; int K = 3; Console.WriteLine(solve(array, n, K)); } } // This code is contributed by Princi Singh |
4
Time Complexity : O(N*log(Sum))
Where N is the number of elements of the array and Sum is the sum of all the elements of the array.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Maximum sub-array sum after dividing array into sub-arrays based on the given queries
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Split array into two subarrays such that difference of their maximum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Ways to divide a binary array into sub-arrays such that each sub-array contains exactly one 1
- Split a given array into K subarrays minimizing the difference between their maximum and minimum
- Split array into maximum subarrays such that every distinct element lies in a single subarray
- Split array into K disjoint subarrays such that sum of each subarray is odd.
- Split array to three subarrays such that sum of first and third subarray is equal and maximum
- Count ways to split array into two equal sum subarrays by replacing each array element to 0 once
- Count ways to split array into two equal sum subarrays by changing sign of any one array element
- Break an array into maximum number of sub-arrays such that their averages are same
- Split array into minimum number of subarrays having GCD of its first and last element exceeding 1
- Split array into subarrays at minimum cost by minimizing count of repeating elements in each subarray
- Minimum index to split array into subarrays with co-prime products
- Split an array into two equal Sum subarrays
- Count of ways to split an Array into three contiguous Subarrays having increasing Sum
- Find sub-arrays from given two arrays such that they have equal sum
- Split array into minimum number of subsets having maximum pair sum at most K
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

