You are given an array 'arr' of 'n' integers.
You have to divide the array into some subarrays such that each element is present in exactly one of the subarrays.
The length of each subarray should be at most 'k'. After partitioning all the elements of each subarray will be changed to the maximum element present in that subarray.
Find the maximum possible sum of all the elements after partitioning.
Note:
Input is given such that the answer will fit in a 32-bit integer.
Input: 'k' = 3, 'arr' = [1, 20, 13, 4, 4, 1]
Output: 80
Explanation:
We can divide the array into the following subarrays
[1,20] max of this subarray is 20 so the contribution of
this subarray in the final answer will be 20*2=40.
[13,4,4] max of this subarray is 13 so the contribution of
this subarray in the final answer will be 13*3=39.
[1] max of this subarray is 1 so the contribution of this
subarray in the final answer will be 1*1=1.
So, the answer will be 40 + 39 + 1 = 80.
The first line of input contains two space-separated integers 'n' and 'k', denoting the size of the array and the given integer.
The second line of each test case contains 'n' space-separated integers denoting elements of the array.
Return the maximum sum of elements after partitioning.
You do not need to print anything, it has already been taken care of. Just implement the given function.
5 2
1 21 1 5 4
56
56
We can divide the array into the following subarrays
[1,21] max of this subarray is 21 so the contribution of this subarray in the final answer will be 21*2=42.
[1,5] max of this subarray is 5 so the contribution of this subarray in the final answer will be 5*2=10.
[4] max of this subarray is 4 so the contribution of this subarray in the final answer will be 1*4=4.
So, the answer will be 42 + 10 + 4 = 56.
.
1 1
6
6
6
Try to solve this in O(n*k).
1 <= 'n' <= 300
0 <= 'arr[i]' <= 10^9
1 <= 'k' <= 'n'
Time limit: 1 sec
Check for every possible valid way of dividing the array and find the max of all valid ways.
The basic idea is to check for every possible valid way of dividing the array and find the max of all valid ways.
For each index ‘i’ we have options of taking a subarray of length [1,2,..,k] from’ i‘ (including ‘i’).
Let’s say we take a subarray starting from ‘i’ and ending to ‘j’ then find the maximum element of the subarray [i,j] let it be ‘max’ so the contribution of this subarray to the final sum will be (j-i+1)*max.
Let the function maxSubarray(i, n, num, k)( ‘i’ is the current index of the array, n is the size of the array, num is the given array, k is the max size of the subarray) give the maximum sum of the subarray [i,n-1],(consider 0-based indexing here) after partitioning. So the final answer will be maxSubarray(0, n, num, k).
Here is the algorithm:
O((2 ^N ) * N), where ‘N’ is the size of the array.
There are 2^N numbers of ways to divide the array and for iterating over all the ‘j’ will take O(N).
O(N), where ‘N’ is the size of the array.
The recursive stack for the “maxSubarray” function can contain at most the size of the array. Therefore, the overall space complexity will be O(N).