- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find length of longest bitonic subsequence in C++

Suppose we have a list of numbers. We have to find length of longest bitonic subsequence. As we knot a sequence is said to be bitonic if it's strictly increasing and then strictly decreasing. A strictly increasing sequence is bitonic. Or a strictly decreasing sequence is bitonic also.

So, if the input is like nums = [0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], size of sequence 16., then the output will be 7.

To solve this, we will follow these steps −

increasingSubSeq := new array of given array size, and fill with 1

for initialize i := 1, when i < size, update (increase i by 1), do −

for initialize j := 0, when j < i, update (increase j by 1), do −

if arr[i] > arr[j] and increasingSubSeq[i] < increasingSubSeq[j] + 1, then −

increasingSubSeq[i] := increasingSubSeq[j] + 1

*decreasingSubSeq := new array of given array size, and fill with 1

for initialize i := size - 2, when i >= 0, update (decrease i by 1), do −

for initialize j := size - 1, when j > i, update (decrease j by 1), do −

if arr[i] > arr[j] and decreasingSubSeq[i] < decreasingSubSeq[j] + 1, then −

decreasingSubSeq[i] := decreasingSubSeq[j] + 1

max := increasingSubSeq[0] + decreasingSubSeq[0] - 1

for initialize i := 1, when i < size, update (increase i by 1), do −

if increasingSubSeq[i] + decreasingSubSeq[i] - 1 > max, then:

max := increasingSubSeq[i] + decreasingSubSeq[i] - 1

return max

Let us see the following implementation to get better understanding −

## Example

#include<iostream> using namespace std; int longBitonicSub( int arr[], int size ) { int *increasingSubSeq = new int[size]; for (int i = 0; i < size; i++) increasingSubSeq[i] = 1; for (int i = 1; i < size; i++) for (int j = 0; j < i; j++) if (arr[i] > arr[j] && increasingSubSeq[i] < increasingSubSeq[j] + 1) increasingSubSeq[i] = increasingSubSeq[j] + 1; int *decreasingSubSeq = new int [size]; for (int i = 0; i < size; i++) decreasingSubSeq[i] = 1; for (int i = size-2; i >= 0; i--) for (int j = size-1; j > i; j--) if (arr[i] > arr[j] && decreasingSubSeq[i] < decreasingSubSeq[j] + 1) decreasingSubSeq[i] = decreasingSubSeq[j] + 1; int max = increasingSubSeq[0] + decreasingSubSeq[0] - 1; for (int i = 1; i < size; i++) if (increasingSubSeq[i] + decreasingSubSeq[i] - 1 > max) max = increasingSubSeq[i] + decreasingSubSeq[i] - 1; return max; } int main() { int arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}; int n = 16; cout << longBitonicSub(arr, n); }

## Input

[0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15], 16

## Output

7

- Related Questions & Answers
- Longest Bitonic Subsequence
- Program to find length of longest balanced subsequence in Python
- Program to find length of longest anagram subsequence in Python
- Program to find length of longest common subsequence in C++
- Program to find length of longest increasing subsequence in Python
- Program to find length of longest palindromic subsequence in Python
- Program to find length of longest fibonacci subsequence in Python
- Program to find length of longest circular increasing subsequence in python
- Program to find length of longest common subsequence of three strings in Python
- Program to find out the length of longest palindromic subsequence using Python
- Program to find length of longest arithmetic subsequence with constant difference in Python
- Program to find length of longest arithmetic subsequence of a given list in Python
- Length of Longest Fibonacci Subsequence in C++
- Program to find length of longest Fibonacci subsequence from a given list in Python
- Program to find length of longest alternating subsequence from a given list in Python