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.
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.
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):
Element Count in Ranges
First Digit One
Minimize Maximum Adjacent Distance
Sorted Doubly Linked List to Balanced BST
Minimized Maximum of Products Distributed to Any Store