


Input: 'arr' = [1, 2, 1, 2, 1]
Output: 3
Explanation: The longest bitonic subsequence for this array will be [1, 2, 1]. Please note that [1, 2, 2, 1] is not a valid bitonic subsequence, because the consecutive 2's are neither strictly increasing, nor strictly decreasing.
The first line contains a single integer 'n' denoting the number of integers in the array.
The second line contains 'n' space-separated integers, denoting the elements of the array.
The output contains an integer, denoting the length of the longest bitonic subsequence.
You don’t need to print anything; it has already been taken care of. Just implement the given function.
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:
We can use memoization to optimize the recursive approach. Since many recursive calls have to be made with the same parameters, this redundancy can be eliminated by storing the results obtained for a particular call in memoization in a matrix ‘memo’.
The algorithm will be:
We can use dynamic programming to find the length of the longest increasing subsequence ending at index ‘i’, and the length of the longest decreasing subsequence beginning at index ‘i’.
The algorithm will be: