You are given an array “NUMS” consisting of N integers and an integer, K. Your task is to determine the maximum sum of an increasing subsequence of length K.
Note:1. The array may contain duplicate elements.
2. The array can also contain negative integers.
3. Every element of the subsequence must be greater than or equal to the previous element.
The subsequence of an array is a sequence of numbers that can be formed by deleting some or no elements without changing the order of the remaining elements. For example, if the given array “NUMS” = {1, 2, 5, 4, 8}, then {1, 2, 5, 4, 8}, {1, 5, 8}, {2} are some of the valid subsequences whereas the sequence {4, 2} is not a valid subsequence as the order of the elements differ from the original array.
The first line contains an integer T, which denotes the number of test cases or queries to be run. Then, the T test cases follow.
The first line of each test case contains two space-separated integers N and K, denoting the number of elements in the array and the length of the subsequence to be considered.
The second line of each test case contains N space-separated integers, representing the elements of the array.
Output Format:
For each test case, print in a new line, an integer denoting the maximum sum of an increasing subsequence of length K.
Print “-1” if no such subsequence exists.
Note:
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 500
1 <= K <= N
-10^9 <= NUMS[i] <= 10^9
Where NUMS[i] represents the i-th element of the array.
Time Limit: 1 sec
1
5 3
1 3 -2 4 5
12
In the first test case, the subsequence having 3(index 1), 4(index 3), and 5(index 4) have the sum of 12, which is the maximum amongst all increasing subsequences having a total of 3 elements. Note that there can be more than one increasing subsequence with the required length, but we have to choose the one which has the maximum sum. In this example, some other such subsequences will be :
1 3 4 sum = 8
1 3 5 sum = 9
1 4 5 sum = 10
3 4 5 sum = 12
-2 4 5 sum = 7
Thus, maximum value of the subsequence is 12.
1
4 3
5 7 4 3
-1
In the first test case, there are a total of 4 subsequences having 3 elements. But none of these subsequences is increasing, so we output “-1”.
Find all subsequences of length K using recursion.
Approach: The approach is to find all subsequences of length ‘K’ using recursion and check whether it is increasing or not. If it is and its sum is more than the maximum sum that we have gotten so far, we update the maximum sum with this subsequence sum.
After we are done with every such subsequence, we return the maximum sum.
This approach might exceed the time limit in most of the test cases.
Algorithm:
void findSubsequences(NUMS, K, CURRINDEX, MAXIMUMSUM, SUBSEQUENCE) :
O(K * nCk), where ‘N’ is the number of elements in the array, and ‘K’ is the length of the subsequence.
There are a total of nCk subsequences of size K, and then for every subsequence, we need linear time to check if it’s increasing or not and to find its sum. So, the overall time complexity will be O(K * nCk).
O(K * nCk), where ‘N’ is the number of elements in the array, and ‘K’ is the length of the subsequence.
There can be at most nCk subsequences of size K, and then for every subsequence, we need extra linear space to store that subsequence. So, the overall time complexity will be O(K * nCk).