Given an integer array ‘A’ of size ‘N’ and an integer ‘K'.
Split the array ‘A’ into ‘K’ non-empty subarrays such that the largest sum of any subarray is minimized.
Your task is to return the minimized largest sum of the split.
A subarray is a contiguous part of the array.
Input: ‘N’ = 5, ‘A’ = [1, 2, 3, 4, 5], ‘K’ = 3
Output: 6
Explanation: There are many ways to split the array ‘A’ into K consecutive subarrays. The best way to do this is to split the array ‘A’ into [1, 2, 3], [4], and [5], where the largest sum among the three subarrays is only 6.
The first line contains one integer, ‘N’, denoting the size of the array ‘A’.
The second line contains ‘N’ integers denoting the elements of the array ‘A’.
The third line contains one integer, ‘K’, denoting the number in which array ‘A’ should be split.
Return the minimized largest sum of the split.
You don't need to print anything. Just implement the given function.
5
1 2 3 4 5
3
6
Input: ‘N’ = 5, ‘A’ = [1, 2, 3, 4, 5], ‘K’ = 3
Output: 6
Explanation: There are many ways to split the array ‘A’ into K consecutive subarrays.
The best way to do this is to split the array ‘A’ into [1, 2, 3], [4], and [5], where the largest sum among the three subarrays is only 6.
3
3 5 1
3
5
Input: ‘N’ = 3, ‘A’ = [3, 5, 1], ‘K’ = 3
Output: 5
Explanation: There is only one way to split the array ‘A’ into 3 subarrays, i.e., [3], [5], and [1]. The largest sum among these subarrays is 5.
6
10 4 5 10 9 10
4
15
1 <= N <= 1e4
1 <= A[i] <= 1e5
1 <= K <= N
Time Limit: 1-sec
Apply Binary Search on the maximum sum which we want to be minimized.
We will use Binary Search here. Binary Search will minimize the maximized sum we can get from the array ‘A’ after breaking it into ‘K’ consecutive subarrays.
We will keep two variables, ‘s’ and ‘e’, initially assigned to ‘1’ and ‘1e9’, respectively. When applying the binary search algorithm, suppose we find the ‘mid’ as ‘(s + e) / 2’. Now, We will determine whether it is possible to array ‘A’ into subarrays such that the sum of each subarray is less than or equal to ‘mid’. Suppose, satisfying this condition, the number of subarrays that will be generated is equal to ‘num’. If ‘num’ is equal to ‘K’, then the answer will be ‘mid’, and to get a smaller answer, we will do ‘e’ = ‘mid - 1’. If ‘num’ is less than ‘K’, we have to increase the number of subarrays that will be generated, so for that, we will do ‘e’ = mid - 1.
Otherwise, We would do ‘s’ = mid + 1, which means ‘num’ is greater than ‘K’, so to decrease the number of subarrays that will be generated, we would increase s, or ‘s’ = mid + 1.
The steps are as follows:-
// Function to check whether we can split the array into subarrays with maximum sum ‘mid’
function isValid(int[] a, int k, int mid):
// Function to find the minimized largest sum of the split.
function largestSubarraySumMinimized(int n, int[] a, int k):
O( N * logM ), where N is the number of elements, and M is the integer(1e9).
We are traversing over the array ‘A’ at most logM times.
Hence the time complexity is O( N * logM ).
O( 1 ).
We store our answers and other things in 2-3 variables.
Hence the space complexity is O( 1 ).