Maximum Sum Subsequence of length k | Practice | GeeksforGeeks

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 Si1<=Si2<=Si3.........<=Sin.
 
Example 1:
Input:
N = 8 K = 3
A[] = {8 5 9 10 5 6 19 8}
Output: 38
Explanation:
Possible increasing subsequence of
length 3 with maximum possible
sum is 9 10 9.
Example 2:
Input:
N = 2,K = 2
A[] = {10 5}
Output: -1
Explanation:
Can't make any increasing subsequence 
of length 2.


Your Task:
You don't need to read or print anything. Your task is to complete the function max_sum() which takes sequence A as the first parameter and K as the second parameter and returns the maximum possible sum of K-length increasing subsequnece. If not possible return -1.
Expected Time Complexity: O(max(Ai) * n * log(max(Ai)))
Expected Space Complexity: O(max(Ai))
Contraints:
1 <= n <= 100
1 <= Ai <= 100000
 
 
0
amitupadhyay4682 months ago
Top down Approach
 
class Solution
{
    int dp[101][101][104];
	public:
	    
		int get(vector<int>&a,int k,int i,int prev)
	    {
	      if(k<0)
	      {
	          return -1e9;
	      }
	        if(i>=a.size())
	        {
	          
	            return (k==0)?0:-1e9;
	        }
	        int ans=-1e9;
	        if(dp[i][k][prev]!=-1)
	        {
	            return dp[i][k][prev];
	        }
	        if(prev==102)
	        {
	            ans=max(ans,get(a,k-1,i+1,i)+a[i]);
	            ans=max(ans,get(a,k,i+1,prev));
	        }
	        else
	        {
	            if(a[prev]<=a[i]&&(k>0))
	            {
	                int res=get(a,k-1,i+1,i)+a[i];
	                int bes=get(a,k,i+1,prev);
	                ans=max(ans,res);
	                ans=max(ans,bes);
	            }
	            else
	            {
	                ans=max(ans,get(a,k,i+1,prev));
	            }
	        }
	        return dp[i][k][prev]=ans;
	    }
		int max_sum(vector<int> &a, int k)
		{
		    memset(dp,-1,sizeof(dp));
		    int x=get(a,k,0,102);
		    if(x<0)
		    {
		        return -1;
		    }
		    return x;
		}
		
		
};
 
0
himanshujain4575 months ago
Correct Answer:
int max_sum(vector<int> &a, int k) {     int l=a.size();     int dp[l+1][k+1];     memset(dp,-1,sizeof(dp));     for(int i=0;i<l;i++)     {         dp[i][1]=a[i];     }     for(int p=2;p<=k;p++)     {         for(int i=1;i<l;i++)         {             for(int j=0;j<i;j++)             {                 if(a[i]>=a[j] and dp[j][p-1]>=0)                 {                     dp[i][p]=max(dp[i][p],a[i]+dp[j][p-1]);                 }             }         }     }     int mx=-1;     for(int i=0;i<l;i++)     {         mx=max(mx,dp[i][k]);     }     return mx; }
0
punitkumarojha7 months ago
// Solve the problem in O(N*N*K) time 

class Solution{

public:
    int max_sum(vector<int> &a, int l){
        if(l>a.size())
            return -1;
            
        int n=a.size();
        int dp[n+1][l+1];
        memset(dp,-1,sizeof(dp));
        
        // initialization for length 1 subsequence
        for(int i=0;i<n;i++)
            dp[i][1]=a[i];
        
        for(int k=2;k<=l;k++)
            for(int i=k-1;i<n;i++)
                for(int j=0;j<i;j++){
                    if(a[j]<=a[i] and dp[j][k-1]>=0)
                        dp[i][k]=max(dp[i][k],a[i]+dp[j][k-1]);
                }
                
        int ans=-1;
        for(int i=0;i<n;i++)
            ans=max(ans,dp[i][l]);
        return ans;
	}
};
0
wallflower10 months ago
wallflower
Easy java backtracking solution
https://ide.geeksforgeeks.o...
0
Shanahan
This comment was deleted.
0
Shanahan
This comment was deleted.
0
Ashutosh Sharma2 years ago
Ashutosh Sharma
intutive dp solution:int max_sum(vector<int> &a, int k){    // Code here    int n=a.size();    int mat[k+1][n+1];    memset(mat,-1,sizeof(mat));    int ans=-1;    for(int i=1;i<=n;i++)        mat[1][i]=a[i-1];    for(int i=2;i<=k;i++) {        for(int j=2;j<=n;j++) {            for(int l=1;l<j;l++) {="" if(a[l-1]<="a[j-1]" and="" mat[i-1][l]!="-1)" {="" mat[i][j]="max(mat[i][j],mat[i-1][l]+a[j-1]);" }="" }="" }="" }="" for(int="" i="0;i&lt;=k;i++)" {="" for(int="" j="0;j&lt;=n;j++)" {="" cout<<mat[i][j]<<"="" ";="" ans="max(ans,mat[k][j]);" }="" cout<<endl;="" }="" return="" ans;="" }<="" code="">
0
hemanth gadarla2 years ago
hemanth gadarla
https://ide.geeksforgeeks.o...using bottom up approach in O(n*n*k)
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.