Ninja bought chocolate consisting of some chunks. The sweetness of each chunk is given in an array ‘ARR’. Ninja has ‘K’ friends, and he wishes to divide the chocolate into 'K' + 1 cut with some consecutive chunks. He wants to divide the chocolate into chunks under the condition that he will take the piece that has the minimum total sweetness.
He is very busy doing other stuff. Can you help him to maximize the total sweetness of the part that you will get under the given condition?
The first line of input contains an integer ‘T’, denoting the number of test cases. Then the test cases follow.
The first line of each test case contains two space-separated integers, ‘N’ and ‘K’, denoting the size of array ‘ARR’ and the number of friends Ninja has.
The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array ‘ARR’.
Output Format:
For each test case, print the maximum sweetness that Ninja can get under the given condition.
Print the output of each test case in a new line.
Note:
You are not required to print the expected output, it has already been taken care of. Just implement the function.
1<= T <= 10
1 <= N <= 50000
0 <= K < N
1 <= ARR[i] <= 10000
Where 'ARR[i]' is the sweetness of the ‘i_th' chunk.
Time Limit: 1 sec
2
9 3
5 6 7 8 9 10 11 12 13
3 2
2 2 2
17
2
In the first test case, the sweetness of the chunks are [5, 6, 7, 8, 9, 10, 11, 12, 13] and Ninja has 3 friends. So, it is to be divided among 4 people including Ninja himself. In order to maximize the total sweetness of Ninja, one possible division is [5, 6, 7], [8, 9], [10, 11], [12, 13]. The minimum sweetness of one part is 17. So, the answer is 17.
In the second test case, the sweetness of the chunks are [2, 2, 2] and Ninja has 2 friends. So, it is to be divided among 3 people, and there are only 3 chunks. So, all three friends will be getting one chunk each. So, the total sweetness that Ninja will get is 2. So, the answer is 2.
2
3 2
5 6 7
10 4
2 3 2 3 2 3 2 3 2 3
5
5
Iterate through every possible value of sweetness that you can have? Try to ignore the subproblems using DP.
The idea is to figure out the recursive calls and optimize them using dynamic programming.
It breaks down into a problem of dividing the array into ‘K’ + 1 partitions such that the partition which has the lowest sum of sweetness is maximum as compared to all the possible partitions. It’s better to make a prefix array as we would need the prefix sum of the array in every iteration.
Now, we will start from the bottom, and then we will find the final answer. We will find the answer for breaking the subarray from indices from 0 to ‘i’ into ‘j’ parts where ‘i’ lies from 0 to ‘N’ - 1 and ‘j’ lies from 0 to a minimum of ‘M’ and ‘i’ + 1.
There is a base condition: If the number of chunks is less than the number of desired divisions, then Ninja won’t get any chunks. So, the answer will be 0.
The steps are as follows:
O((N^2)*K), where ‘K’ denotes the number of friends and ‘N’ is the number of chunks.
As we are iterating over all the elements and then creating all possible divisions from 2 to ‘K’ and then choosing the maximum answer from all the possible divisions from 0 to ‘i’ - 1. Hence, the overall time complexity is O((N^2)*K).
O(N*K), where K denotes the number of friends and N is the number of chunks.
As we are using a 2D matrix of size N*(K+1) to store the results for breaking the subarray from indices 0 to n - 1 into K parts. Hence, the overall space complexity is O(N*K).