# 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,

Live Demo

#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);
cout<<"The Maximum Sum of Bi-tonic subsequence is : "<<findMaxSumBiTonicSubSeq(arr, N);
return 0;
}

## Output

The Maximum Sum of Bi-tonic subsequence is : 33