Given an array sequence [A1, A2 …An], the task is to find the maximum possible sum of increasing subsequence S of length k such that S1<=S2<=S3………<=Sk.
n = 8 k = 3
A=[8 5 9 10 5 6 21 8]
Output : 40
Possible Increasing subsequence of Length 3 with maximum possible sum is 9 10 21
n = 9 k = 4
A=[2 5 3 9 15 33 6 18 20]
Output : 62
Possible Increasing subsequence of Length 4 with maximum possible sum is 9 15 18 20
One thing that is clearly visible that it can be easily solved with dynamic programming and this problem is a simple variation of Longest Increasing Subsequence. If you are unknown of how to calculate the longest increasing subsequence then see the implementation going to the link.
In the brute force approach, first we will try to find all the subsequences of length k and will check whether they are increasing or not. There could be nCk such sequences in the worst case when all elements are in increasing order. Now we will find the maximum possible sum for such sequences.
Time Complexity would be O((nCk)*n).
We will be using a two-dimensional dp array in which dp[i][l] means that maximum sum subsequence of length l taking array values from 0 to i and the subsequence is ending at index ‘i’. Range of ‘l’ is from 0 to k-1. Using the approach of longer increasing subsequence on the inner loop when j<i we will check if arr[j] < arr[i] for checking if subsequence increasing.
This problem can be divided into its subproblems:
dp[i]=arr[i] for length 1 , maximum icreasing subsequence is equal to the array value
dp[i][l+1]= max(dp[i][l+1], dp[j][l]+arr[i]) for any length l between 1 to k-1
This means that if for ith position and subsequence of length l+1 , there exists some subsequence at j (j < i) of length l for which sum of dp[j][l] + arr[i] is more than its initial calculated value then update that value.
Then finally we will find the maximum value of dp[i][k] i.e for every ‘i’ if subsequence of k length is causing more sum than update the required ans.
Below is the implementation code:
Time complexity: O(n^2*k)
Space complexity: O(n^2)
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum length subsequence possible of the form R^N K^N
- Maximum Bitwise AND value of subsequence of length K
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Find the Increasing subsequence of length three with maximum product
- Maximum length prefix of one string that occurs as subsequence in another
- Subsequence X of length K such that gcd(X, X) + (X, X) + ... is maximized
- Repeated subsequence of length 2 or more
- Count subsequence of length three in a given string
- Length of Longest Balanced Subsequence
- Number of balanced bracket subsequence of length 2 and 4
- Length of longest strict bitonic subsequence
- Find length of longest Fibonacci like subsequence
- Length of longest common subsequence containing vowels
- Length of the longest subsequence such that xor of adjacent elements is non-decreasing
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.