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)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Maximum length subsequence such that adjacent elements in the subsequence have a common factor
- Maximum length Subsequence with alternating sign and maximum Sum
- Count maximum occurrence of subsequence in string such that indices in subsequence is in A.P.
- Length of longest Palindromic Subsequence of even length with no two adjacent characters same
- Find the Increasing subsequence of length three with maximum product
- Maximum length prefix of one string that occurs as subsequence in another
- Maximum length subsequence with difference between adjacent elements as either 0 or 1
- Maximum length subsequence possible of the form R^N K^N
- Maximum Bitwise AND value of subsequence of length K
- Maximum bitwise OR value of subsequence of length K
- Length of Smallest Subsequence such that sum of elements is greater than equal to K
- Maximize product of digit sum of consecutive pairs in a subsequence of length K
- Check if a subsequence of length K with odd sum exists
- Longest subsequence such that every element in the subsequence is formed by multiplying previous element with a prime
- Longest Increasing Subsequence using Longest Common Subsequence Algorithm
- Find the equal pairs of subsequence of S and subsequence of T
- Maximum Sum Increasing Subsequence | DP-14
- Printing Maximum Sum Increasing Subsequence
- Maximum sum alternating subsequence
- Maximum subsequence sum such that no three are consecutive
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.