![]() |
VOOZH | about |
Given an array arr[] containing n positive integers, find the length of the longest bitonic subsequence. A subsequence of numbers is called bitonic if it is first strictly increasing, then strictly decreasing.
Note: Only strictly increasing (no decreasing part) or a strictly decreasing sequence should not be considered as a bitonic sequence.
Examples:
Input: n = 5, nums[] = [1, 2, 5, 3, 2]
Output: 5
Explanation: The sequence [1, 2, 5] is increasing and the sequence [3, 2] is decreasing so merging both we will get length 5.Input: n = 8, nums[] = [1, 11, 2, 10, 4, 5, 2, 1]
Output: 6
Explanation: The bitonic sequence [1, 2, 10, 4, 2, 1] has length 6.
Table of Content
In this approach, we consider every element of the array as a possible peak of a bitonic subsequence.
For an element to be a valid peak:
- The elements on its left must form a strictly increasing subsequence.
- The elements on its right must form a strictly decreasing subsequence.
A sequence that is only increasing or only decreasing does not qualify as bitonic, because a bitonic subsequence must have both parts with a clear turning point (the peak).
So, for each index i, we calculate:
This gives us the bitonic subsequence length with i as the peak.
Finally, we take the maximum bitonic length among all choices of peaks.
5
We treat every element as a potential peak of a bitonic sequence. For each peak, we recursively find the longest decreasing subsequence on the left and right sides using memoization to avoid recomputation. At each step, we decide whether to include the current element based on the decreasing condition. If both left and right parts exist, we combine their lengths with the peak (
left + right + 1). Finally, we return the maximum length among all such valid peaks.
Algorithm:
left + right + 1. 5
We compute the LIS from left to right and the LDS from right to left. For each index i, LIS is formed by extending previous smaller elements (j < i), while LDS is formed by extending smaller elements on the right (j > i). Then, treating each index as a peak, the bitonic length is calculated as LIS[i] + LDS[i] - 1. We consider only those indices where both LIS[i] > 1 and LDS[i] > 1, and return the maximum value among them.
Algorithm:
leftLIS[], where each index stores the longest increasing subsequence ending at that position. rightLDS[], where each index stores the longest decreasing subsequence starting from that position. LIS + LDS - 1 to get the bitonic length. 5