# Maximum sum bitonic subarray in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

66 Lectures 5.5 hours

In this problem, we are given an array arr[]. Our task is to create a program to find the maximum sum bitonic subarray in C++.

Bitonic Subarray is a special subarray in which the element strictly increase first and then strictly decreases after reaching a certain point.

Let’s take an example to understand the problem,

## Input

arr[] = {4, 2, 3, 7 ,9, 6, 3, 5, 1}

## Output

30

## Explanation

The bitonic subarray is [2, 3, 7, 9, 6, 3]. Sum = 2 + 3 + 7 + 9 + 6 + 3 = 30

## Solution Approach

The solution is similar to that in the bitonic subsequence problem. We will create two arrays incSubArr[] and decSubArr[]. That will create store increasing and decreasing subarrays. At index i, incSubArr[i] will find increasing subarray from 0 to i. And decSubArr[i] will find increasing subarray from i to N.

The maxSum is the maximum value calculated as (incSubArr[i] + decSubArr[i] - arr[i]).

## Example

Program to illustrate the working of our solution,

Live Demo

#include <iostream>
using namespace std;
int findMaxSumBiTonicSubArr(int arr[], int N){
int incSubArr[N], decSubArr[N];
int max_sum = -1;
incSubArr[0] = arr[0];
for (int i=1; i<N; i++)
if (arr[i] > arr[i-1])
incSubArr[i] = incSubArr[i-1] + arr[i];
else
incSubArr[i] = arr[i];
decSubArr[N-1] = arr[N-1];
for (int i= (N-2); i>=0; i--)
if (arr[i] > arr[i+1])
decSubArr[i] = decSubArr[i+1] + arr[i];
else
decSubArr[i] = arr[i];
for (int i=0; i<N; i++)
if(max_sum < (incSubArr[i] + decSubArr[i] - arr[i]))
max_sum = incSubArr[i] + decSubArr[i] - arr[i];
return max_sum;
}
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 Bitonic Subarray is "<<findMaxSumBiTonicSubArr(arr, N);
return 0;
}

## Output

The Maximum Sum of Bitonic Subarray is 30
Updated on 15-Oct-2020 14:00:02