
- 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 alternating subsequence in C++ program
In this problem, we are given an array arr[] of n integers. Our task is to create a program to find the Maximum sum alternating subsequence starting from the first element of the array.
An alternating subsequence is a subsequence in which elements are increasing and decreasing in an alternating order i.e. first decreasing, then increasing, then decreasing. Here, the reverse alternating subsequence is not valid for finding the maximum sum.
Let’s take an example to understand the problem,
Input
arr[] = {5, 1, 6, 2, 4, 8, 9}
Output
27
Explanation
Starting element: 5, decrease: 1, increase: 6, decrease: 2, increase:4, N.A. Here, we can use 4, 8, 9 as the last element of the subsequence. Sum = 5 + 1 + 6 + 2 + 4 + 9 = 27
Solution Approach
To solve the problem, we will be using a dynamic programming approach. For this, we will be using two arrays one to store the maximum sum of elements ending with arr[i], where arr[i] is increasing. Other to store the maximum sum of elements ending with arr[i], where arr[i] is decreasing.
Then we will add elements one by checking if they are alternating subsequences. For each array, we will calculate the maximum sum until the index. And the return the maximum value after traversing n elements.
Example
Program to illustrate the working of our solution,
#include<iostream> #include<cstring> using namespace std; int maxVal(int x, int y){ if(x > y) return x; return y; } int calcMaxSumAltSubSeq(int arr[], int n) { int maxSum = −10000; int maxSumDec[n]; bool isInc = false; memset(maxSumDec, 0, sizeof(maxSumDec)); int maxSumInc[n]; memset(maxSumInc, 0, sizeof(maxSumInc)); maxSumDec[0] = maxSumInc[0] = arr[0]; for (int i=1; i<n; i++) { for (int j=0; j<i; j++) { if (arr[j] > arr[i]) { maxSumDec[i] = maxVal(maxSumDec[i], maxSumInc[j]+arr[i]); isInc = true; } else if (arr[j] < arr[i] && isInc) maxSumInc[i] = maxVal(maxSumInc[i], maxSumDec[j]+arr[i]); } } for (int i = 0 ; i < n; i++) maxSum = maxVal(maxSum, maxVal(maxSumInc[i], maxSumDec[i])); return maxSum; } int main() { int arr[]= {8, 2, 3, 5, 7, 9, 10}; int n = sizeof(arr)/sizeof(arr[0]); cout<<"The maximum sum alternating subsequence starting is "<<calcMaxSumAltSubSeq(arr , n); return 0; }
Output
The maximum sum alternating subsequence starting is 25
- Related Articles
- Maximum sum alternating subsequence in C++
- Maximum Sum Increasing Subsequence using DP in C++ program
- Maximum Sum Decreasing Subsequence in C++
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++ program
- Maximum Sum Increasing Subsequence | DP-14 in C++
- Maximum subsequence sum such that no three are consecutive in C++ Program
- Maximum sum subsequence with at-least k distant elements in C++ program
- Maximum Sum Increasing Subsequence\n
- Maximum Sum Increasing Subsequence using Binary Indexed Tree in C++
- Maximum sum subsequence with at-least k distant elements in C++
- Maximum product of an increasing subsequence in C++ Program
- Constrained Subsequence Sum in C++
- Program to find length of longest alternating subsequence from a given list in Python
- Sum of Subsequence Widths in C++
- Maximum subsequence sum such that no three are consecutive
