
- 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 Bi-tonic Sub-sequence in C++
In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum Bi-tonic subsequence in C++.
Bi-tonic subsequence is a special sequence whose elements first increase and then decrease.
Let’s take an example to understand the problem,
Input
arr[] = {4, 2, 3, 7, 9, 6, 3, 5, 1}
Output
33
Explanation
The Bi-tonic subsequence which gives the largest sum is {2, 3, 7, 9, 6, 5, 1} Sum = 2 + 3 + 7 + 9 + 6 + 5 + 1 = 33
Solution Approach
To find the maximum sum bitonic subsequence, we will create two arrays, incSeq[] and decSeq[] in such a way that for an element i at index, incSeq[i] has sum of all elements from arr[0…i] strictly increasing and decSeq[i] has sum of all elements from arr[i…n] strictly decreasing.
At the end, we will return the maxSum as maximum value from (incSeq[i] + decSeq[i] - arr[i]).
Example
Program to illustrate the wording of our solution,
#include <iostream> using namespace std; int calcMaxVal(int a, int b){ if(a > b) return a; return b; } int findMaxSumBiTonicSubSeq(int arr[], int N){ int maxSum = -1; int incSeq[N], decSeq[N]; for (int i = 0; i < N; i++){ decSeq[i] = arr[i]; incSeq[i] = arr[i]; } for (int i = 1; i < N; i++) for (int j = 0; j < i; j++) if (arr[i] > arr[j] && incSeq[i] < incSeq[j] + arr[i]) incSeq[i] = incSeq[j] + arr[i]; for (int i = N - 2; i >= 0; i--) for (int j = N - 1; j > i; j--) if (arr[i] > arr[j] && decSeq[i] < decSeq[j] + arr[i]) decSeq[i] = decSeq[j] + arr[i]; for (int i = 0; i < N; i++) maxSum = calcMaxVal(maxSum, (decSeq[i] + incSeq[i] - arr[i])); return maxSum; } int main(){ int arr[] = {4, 2, 3, 7, 9, 6, 3, 5, 1}; int N = sizeof(arr) / sizeof(arr[0]); cout<<"The Maximum Sum of Bi-tonic subsequence is : "<<findMaxSumBiTonicSubSeq(arr, N); return 0; }
Output
The Maximum Sum of Bi-tonic subsequence is : 33
- Related Articles
- Maximum product quadruple (sub-sequence of size 4) in array in C++
- Finding the sub array that has maximum sum JavaScript
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++
- Maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array in C++ program
- Print maximum sum square sub-matrix of given size in C Program.
- Maximum OR sum of sub-arrays of two different arrays in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++
- Maximum sub-tree sum in a Binary Tree such that the sub-tree is also a BST in C++ program
- Sum MongoDB Sub-documents field?
- Find maximum length Snake sequence in Python
- Find maximum length Snake sequence in C++
- Find sum of sum of all sub-sequences in C++
- Find maximum length sub-list in a nested list in Python
- Find sub-matrix with the given sum in C++
- What Is Bi-quadratic Polynomial?
