
- 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 from a prefix and a given element after prefix is must in C++
In this problem, we are given an array arr[] of N integers and two index values x and y. Our task is to create a program to find the Maximum sum increasing subsequence from a prefix and a given element after prefix is must in C++.
Problem Description
We will find the maximum sum of increasing sequence till index x and including the element at index y.
Let’s take an example to understand the problem,
Input
arr[] = {1, 5, 9, 131, 6, 100, 11, 215}, x = 4, y = 6
Output
26
Explanation
We will take the subsequence till index 3 and then at last include arr[6] = 11.
The subsequence is {1, 5, 9, 11}. Sum = 1+5+9+11 = 26
Solution Approach
A simple approach is creating a new array till x index and then adding the element at index y at the end. Then calculate all increasing subsequences and then discard all that cannot include the element arr[y], and find the maxSum.
One more effective approach to solve the problem is using a dynamic programming approach. The idea is to create a 2-D array DP[][], and store the maximum sum of increasing subsequence. The value at DP[x][y] will give the max sum till index x including element arr[y].
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int DP[100][100]; void preCalcMaxSum(int arr[], int N){ for (int i = 0; i < N; i++) { if (arr[i] > arr[0]) DP[0][i] = arr[i] + arr[0]; else DP[0][i] = arr[i]; } for (int i = 1; i < N; i++) { for (int j = 0; j < N; j++) { if (arr[j] > arr[i] && j > i) { if (DP[i - 1][i] + arr[j] > DP[i - 1][j]) DP[i][j] = DP[i - 1][i] + arr[j]; else DP[i][j] = DP[i - 1][j]; } else DP[i][j] = DP[i - 1][j]; } } } int main() { int arr[] = {1, 5, 9, 131, 6, 100, 11, 215}; int N = sizeof(arr) / sizeof(arr[0]); int x = 4, y = 6; preCalcMaxSum(arr, N); cout<<"The maximum sum increasing subsequence from a prefix and a given element after prefix is must is "; cout<<DP[x][y]; return 0; }
Output
The maximum sum increasing subsequence from a prefix and a given element after prefix is must is 26
An efficient approach is using to find the maximum sum of increasing subsequence till index x in such a way that the largest element of the sequence is less than element at index y. For this we will again use the dynamic programming approach.
Example
Program to illustrate the working of our solution,
#include <iostream> using namespace std; int calcMaxSum(int arr[], int n, int x, int y){ int DP[x] = {0}; int maxSum = -1; for (int i = 0; i <= x; i++) DP[i] = arr[i]; for (int i = 0; i <= x; i++) { if (arr[i] >= arr[y]) { continue; } for (int j = 0; j < i; j++) { if (arr[i] > arr[j]) DP[i] += arr[j]; maxSum = max(maxSum, DP[i]); } } if (maxSum == -1) { return arr[y]; } return maxSum + arr[y]; } int main(){ int arr[] = {1, 5, 9, 131, 6, 100, 11, 215}; int N = sizeof(arr) / sizeof(arr[0]); int x = 4, y = 6; cout<<"The maximum sum increasing subsequence from a prefix and a given element after prefix is must is "; cout<<calcMaxSum(arr, N, x, y); return 0; }
Output
The maximum sum increasing subsequence from a prefix and a given element after prefix is must is 26
- Related Articles
- Maximum prefix-sum for a given range in C++
- Maximum Sum Increasing Subsequence\n
- Maximum subarray sum in O(n) using prefix sum in C++
- Maximum Sum Increasing Subsequence | DP-14 in C++
- Prefix sums (Creating an array with increasing sum) with Recursion in JavaScript
- Python - Prefix sum list
- Maximum Sum Increasing Subsequence using DP in C++ program
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program
- How to find strings with a given prefix in MySQL?
- Maximum occurrence of prefix in the Array in C++
- Prefix sum array in python using accumulate function
- Find the Number of Prefix Sum Prime in Given Range Query using C++
- Prefix Sum of Matrix (Or 2D Array) in C++
- Return a boolean array where the string element in array begins with a given prefix but test begin and end in Python
