Skip to content

Commit 5e29351

Browse files
committed
Maximum Sum Increasing Subsequence
1 parent b60b336 commit 5e29351

File tree

2 files changed

+49
-0
lines changed

2 files changed

+49
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
using namespace std;
3+
4+
int MaxSumIncrsSubseq(int arr[],int n)
5+
{
6+
int i,j,mx=0;
7+
int MSIS[n]; //An array of initial size as that of the original array to store the maximum sum of the increasing subsequence till that point.
8+
for(i=0;i<n;i++)
9+
{
10+
MSIS[i]=arr[i];
11+
}
12+
/* The main condition to check if the next element of the array should be considered in the maximum sum subsequence array or not ,
13+
by checking the given two conditions:-
14+
1.) arr[i]>arr[j]
15+
2.)MSIS[i]<MSIS[j] + arr[i]
16+
*/
17+
for(i=1;i<n;i++)
18+
{
19+
for(j=0;j<i;j++)
20+
{
21+
if(arr[i]>arr[j] && MSIS[i]<MSIS[j]+arr[i])
22+
MSIS[i]=MSIS[j]+arr[i];
23+
}
24+
}
25+
for(i=0;i<n;i++)
26+
if(mx<MSIS[i])
27+
mx=MSIS[i];
28+
29+
return mx; // The maximum sum of the increasing subsequence.
30+
}
31+
32+
int main()
33+
{
34+
int arr[] = {2,3,100,5,6,101,1};
35+
int n = sizeof(arr)/sizeof(arr[0]);
36+
int ans = MaxSumIncrsSubseq(arr,n);
37+
cout<<ans<<"\n";
38+
return 0;
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
Maximum Sum in an increasing subsequence - Dynamic Programming
2+
3+
-This algorithm is a slight variation in the Longest Increasing Subsequence.
4+
-In this algorithm we insert a case to check whether the sum of the new increasing subsequence is greater than the previous one or not.
5+
6+
Example:-
7+
8+
arr[] = {2,3,100,5,6,101,1}
9+
10+
Now, here the maximum sum increasing subsequence will be = 206.

0 commit comments

Comments
 (0)