
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Maximum Sum Increasing Subsequence using DP in C++ program
In this problem, we are given an array arr[] of size n. Our task is to create a program to find the maximum Sum Increasing Subsequence using DP in C++.
Problem Description − To find the maximum sum increasing subsequence, we will be creating a subsequence in which the next element is greater than the current element.
Let’s take an example to understand the problem,
Input
arr[] = {4, 2, 3, 6, 5, 9}
Output
20
Explanation
Increasing subsequence with maximum sum: {2, 3, 6, 9} = 2 + 3 + 6 + 9 = 20
Solution Approach
To solve the problem using a dynamic Program Approach. We will create an array to store the maximum sum until the current element. Then return the maxSum from the array.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int retMaxVal(int x, int y){ if(x > y) return x; return y; } int calcMaxSubSeqSum(int arr[], int n) { int maxSum = 0; int sumDP[n]; for (int i = 0; i < n; i++ ) sumDP[i] = arr[i]; for (int i = 1; i < n; i++ ) for (int j = 0; j < i; j++ ) if ( (sumDP[i] < (sumDP[j] + arr[i])) && ( arr[i] > arr[j] ) ) sumDP[i] = sumDP[j] + arr[i]; for (int i = 0; i < n; i++ ) maxSum = retMaxVal(sumDP[i], maxSum); return maxSum; } int main() { int arr[] = {4, 2, 3, 6, 5, 9}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"The maximum Sum Increasing Subsequence using DP is "<<calcMaxSubSeqSum(arr, n); return 0; }
Output
Sum of maximum sum increasing subsequence is 20
- Related Articles
- Maximum Sum Increasing Subsequence | DP-14 in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program
- Maximum Sum Increasing Subsequence\n
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++
- Maximum product of an increasing subsequence in C++ Program
- Maximum sum alternating subsequence in C++ program
- Maximum product of an increasing subsequence in C++
- Maximum product of an increasing subsequence of size 3 in C++ program
- Maximum sum alternating subsequence in C++
- Maximum Sum Decreasing Subsequence in C++
- Java Program for Longest Increasing Subsequence
- JavaScript Program to Find the Longest Bitonic Subsequence | DP-15
- Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++
- Maximum product of an increasing subsequence of size 3 in C++
- Maximum sum rectangle in a 2D matrix | DP-27 in C++

Advertisements