Skip to content

Commit

Permalink
Merge pull request #6668 from ManviGoel26/max-sum-sub
Browse files Browse the repository at this point in the history
Adding C++ code for Maximum Sum of Increasing subsequence
  • Loading branch information
AdiChat authored Oct 31, 2022
2 parents f47a246 + 1024748 commit b9b256c
Showing 1 changed file with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <bits/stdc++.h>
#include <vector>
using namespace std;

int maxSum(int arr[], int n){
int i, j, max = 0;
vector<int> dp(n);

for (i = 0; i < n; i++){
dp[i] = arr[i];
}

for (i = 1; i < n; i++ ){
for (j = 0; j < i; j++ ){
if (arr[i] > arr[j] && dp[i] < dp[j] + arr[i]){
dp[i] = dp[j] + arr[i];
}
}
}

for (i = 0; i < n; i++){
if (max < dp[i]){
max = dp[i];
}
}
return max;
}

// Driver Code
int main()
{
int arr[] = {4, 6, 1, 3, 8, 4, 6};
int n = sizeof(arr) / sizeof(arr[0]);
cout << "Sum of maximum sum increasing subsequence is " << maxSum(arr, n) << ".\n";

return (0);
}

0 comments on commit b9b256c

Please sign in to comment.