 New update is available. Click here to update.

# Longest Bitonic Sequence

Last Updated: 2 Jan, 2021
Difficulty: Moderate

## PROBLEM STATEMENT

#### For Example:

``````An example of a bitonic sequence will be 1 < 2 < 3 < 4 > 2 > 1.
``````

#### For Example:

``````Let ARR = [1, 2, 1, 2, 1]

One of the bitonic subsequences for this array will be [1, 2, 2, 1].
``````
##### Input Format
``````The first line of input contains an integer ‘T’ denoting the number of test cases to run. Then the test case follows.

The first line of each test case contains a single integer ‘N’ denoting the number of integers in the array/list.

The second line of each test case contains ‘N’ single space-separated integers, denoting the elements of the array.
``````
##### Output Format :
``````For each test case, print an integer denoting the length of the longest bitonic sequence.

Output for each test case will be printed in a new line.
``````
##### Note:
``````You don’t need to print anything; it has already been taken care of. Just implement the given function.
``````
##### Constraints:
``````1 <= T <= 5
1 <= N <= 10^3
1 <= ARR[i] <= 10^5

Time Limit: 1sec
`````` ## Approach 1

The key observation here is that for each index ‘i’, of ‘ARR’ the length of the bitonic sequence containing index ‘i’, will be the sum of the length of the longest increasing subsequence ending at ‘i’, and the length of longest decreasing subsequence beginning at ‘i’. We can use a recursive approach for finding the length of the longest increasing and decreasing subsequence.

The algorithm will be:

1. We will iterate through the array.
2. For finding out the length of the longest increasing sequence ending at i :
• We will call a recursion ‘longestIncreasing’ keeping the current index, the last element included in the subsequence as the parameter. Let that element be ‘LAST’.
• In each recursive call:
• If (‘ARR[CURRINDEX]’ > ‘LAST’) we include ‘ARR[CURRINDEX]’ in our subsequence and recur again.
• Else, we recur without including ‘ARR[CURRINDEX]’ in our subsequence.
3. For finding out the length of the longest decreasing sequence beginning at ‘i’ :
• We will call a  different recursion ‘longestDecreasing’ keeping the current index, the last element included in the subsequence as the parameter. Let that element be ‘LAST’.
• In each recursive call, we will:
• If (‘ARR[CURRINDEX]’ < ‘LAST’) we include ‘ARR[CURRINDEX]’ in our subsequence and recur again.
• Else, we call recur without including ‘ARR[CURRINDEX]’ in our subsequence.
4. The length of the longest bitonic sequence containing index ‘i’, will be the longest increasing subsequence containing ‘i’ + longest decreasing subsequence containing index ‘i’ - 1.
5. The maximum value of the bitonic sequence encountered will be our answer.
SIMILAR PROBLEMS

SICK NINJA

Posted: 6 Sep, 2022
Difficulty: Hard

JUMP GAME

Posted: 8 Sep, 2022
Difficulty: Moderate

NINJA AND HAPPINESS

Posted: 9 Sep, 2022
Difficulty: Hard

DECODE STRING

Posted: 11 Sep, 2022
Difficulty: Moderate

Randomly Sorted

Posted: 13 Nov, 2022
Difficulty: Moderate