-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6668 from ManviGoel26/max-sum-sub
Adding C++ code for Maximum Sum of Increasing subsequence
- Loading branch information
Showing
1 changed file
with
37 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
...programming/src/maximum_sum_increasing_subsequence/maximum_sum_increasing_subsequence.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |