Maximum sum increasing subsequence | Practice | GeeksforGeeks

Given an array of n positive integers. Find the sum of the maximum sum subsequence of the given array such that the integers in the subsequence are sorted in increasing order i.e. increasing subsequence. 
Example 1:
Input: N = 5, arr[] = {1, 101, 2, 3, 100} 
Output: 106
Explanation:The maximum sum of a
increasing sequence is obtained from
{1, 2, 3, 100}
Example 2:
Input: N = 3, arr[] = {1, 2, 3}
Output: 6
Explanation:The maximum sum of a
increasing sequence is obtained from
{1, 2, 3}

Your Task:  
You don't need to read input or print anything. Complete the function maxSumIS() which takes N and array arr as input parameters and returns the maximum value.

Expected Time Complexity: O(N2)
Expected Auxiliary Space: O(N)

Constraints:
1 ≤ N ≤ 103
1 ≤ arr[i] ≤ 105
+1
sanketbhagat1 week ago
SIMPLE JAVA SOLUTION
class Solution{
	public int maxSumIS(int arr[], int n)  {  
	    //code here.
	    int dp[] = new int[n];
	    for(int i=0;i<n;i++) dp[i] = arr[i];
	    int ans = arr[0];
	    for(int i=1;i<n;i++){
	        for(int j=0;j<i;j++){
	            if(arr[i]>arr[j]){
	                dp[i] = Math.max(dp[i],arr[i]+dp[j]);
	            }
	        }
	        ans = Math.max(ans,dp[i]);
	    }
	    return ans;
	}  
}
0
milindprajapatmst192 weeks ago
class Solution{
	public:
	unordered_map<int, int> M;
	int maxSumIS(int arr[], int n) {  
	    for (int i = 0; i < n; i++) {
	        M[arr[i]] = max(M[arr[i]], arr[i]);
	        for (auto& itr : M) {
	            if (itr.first < arr[i]) 
	                M[arr[i]] = max(M[arr[i]], itr.second + arr[i]);
	        }
	    }
	    int result = 0;
	    for (auto& itr : M)
	        result = max(result, itr.second);
	    return result;
	}  
};
0
jainmuskan5653 weeks ago
int lis_help(int prev,int curr, int arr[],int n,vector<vector<int>> &dp){    if(curr==n){        return 0;    }    if(dp[prev+1][curr]!=-1){        return dp[prev+1][curr];    }    int maxSum= lis_help(prev,curr+1,arr,n,dp);    if(prev==-1 || arr[prev]<arr[curr]){        maxSum=max(maxSum,arr[curr]+lis_help(curr,curr+1,arr,n,dp));    }    return dp[prev+1][curr]= maxSum;}int maxSumIS(int arr[], int n)  {      vector<vector<int>> dp(n+1,vector<int> (n+1,-1));    return lis_help(-1,0,arr,n,dp);}
+1
adityagagtiwari1 month ago
Ez pz java code. Runs in nlogn. 
class Solution{public int maxSumIS(int arr[], int n)  {      //code here.    int[] dp =new int[n];    int max = 0;    dp[0] = arr[0];    for(int i=1;i<n;i++)    {        max = 0;        for(int j=0;j<i;j++)        {            if(arr[i]>arr[j])            {                max = Math.max(dp[j],max);            }        }        dp[i] = max + arr[i];    }    max = 0;    for(int i=0;i<n;i++)    {        max = Math.max(dp[i],max);    }    return max;}  }
0
officialshivaji0071 month ago
 int lis(int n,int cur,int maxTillNowIdx,int a[],vector<vector<int>> &dp){       if(cur == n-1) {           if(maxTillNowIdx == -1){               return a[cur];           }           if(a[cur]> a[maxTillNowIdx]){               return a[cur];           }else return 0;       }              if(dp[cur][maxTillNowIdx+1]!=-1) return dp[cur][maxTillNowIdx+1];                     if(maxTillNowIdx == -1){            return dp[cur][maxTillNowIdx+1]= max(a[cur]+lis(n,cur+1,cur,a,dp),lis(n,cur+1,maxTillNowIdx,a,dp));       }          if(a[cur]> a[maxTillNowIdx]){           return dp[cur][maxTillNowIdx+1]=max(a[cur]+lis(n,cur+1,cur,a,dp),lis(n,cur+1,maxTillNowIdx,a,dp));       }              return dp[cur][maxTillNowIdx+1]=lis(n,cur+1,maxTillNowIdx,a,dp);   }
public:int maxSumIS(int arr[], int n)  {      // Your code goes here    vector<vector<int>> dp(n+2,vector<int>(n+2,-1));       return lis(n,0,-1,arr,dp);}  
0
hamidnourashraf1 month ago
class Solution:
	def maxSumIS(self, Arr, n):
		dp = Arr.copy()
		for i in range(1, n):
		    for j in range(0, i):
		        if Arr[i] > Arr[j]:
		            dp[i] = max(dp[i], dp[j]+Arr[i])
		return max(dp)
0
annanyamathur1 month ago
int maxSumIS(int arr[], int n)  
	{  
	    int maxis[n];
	    maxis[0]=arr[0];
	    for(int i=1;i<n;i++)
	    { maxis[i]=arr[i];
	        for(int j=0;j<i;j++)
	        {
	            if(arr[j]<arr[i])
	            maxis[i]=max(maxis[i],maxis[j]+arr[i]);
	        }
	    }
	    int res=maxis[0];
	    for(int i=1;i<n;i++)
	    {
	        res=max(res,maxis[i]);
	    }
	    
	    return res;
	}  
0
ash_code2 months ago
testcases are wrong GFGHow come the answer for the following testcase is 138 ? It should be 130 for the subsequence [20, 27, 37, 46]
7
20 8 27 37 9 12 46
0
raghav20892 months ago
int solve(int prev, int currIndex, int arr[], int n, vector<vector<int>> &dp) {    if(currIndex >= n) return 0;        if(dp[prev + 1][currIndex] != -1) return dp[prev + 1][currIndex];        int notPick = solve(prev, currIndex + 1, arr, n, dp);        int pick = 0;        if(prev == -1 or arr[prev] < arr[currIndex]) {        pick = arr[currIndex] + solve(currIndex, currIndex + 1, arr, n, dp);    }        return dp[prev + 1][currIndex] = max(pick, notPick);}int maxSumIS(int arr[], int n)  {      // Your code goes here    vector<vector<int>> dp(n + 1, vector<int>(n+1, -1));    return solve(-1, 0, arr, n, dp);} 
0
lindan1232 months ago
public:
	int maxSumIS(int arr[], int n)  
	{  
	    int dp[n+1];
	    int ans = arr[0];
	    
	    if(n==1)
	    {
	        return arr[0];
	    }
	    
	    for(int i=0;i<n;i++)
	    {
	        dp[i] = arr[i];
	    }
	    
	    for(int i=1;i<n;i++)
	    {
	        for(int j=0;j<i;j++)
	        {
	            if(arr[i]>arr[j] and dp[j]+arr[i]>dp[i])
	            {
	                dp[i] = dp[j]+arr[i];
	            }
	        }
	        ans = max(ans,dp[i]);
	    }
	    return ans;
	}  
Time Taken : 0.0
Cpp
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.