Given an array arr[0 … n-1] containing n positive integers, a subsequence of arr[] is called Bitonic if it is first increasing, then decreasing. Write a function that takes an array as argument and returns the length of the longest bitonic subsequence.
A sequence, sorted in increasing order is considered Bitonic with the decreasing part as empty. Similarly, decreasing order sequence is considered Bitonic with the increasing part as empty.

Examples:

Input arr[] = {1, 11, 2, 10, 4, 5, 2, 1};
Output: 6 (A Longest Bitonic Subsequence of length 6 is 1, 2, 10, 4, 2, 1)

Input arr[] = {12, 11, 40, 5, 3, 1}
Output: 5 (A Longest Bitonic Subsequence of length 5 is 12, 11, 5, 3, 1)

Input arr[] = {80, 60, 30, 40, 20, 10}
Output: 5 (A Longest Bitonic Subsequence of length 5 is 80, 60, 30, 20, 10)

Solution
This problem is a variation of standard Longest Increasing Subsequence (LIS) problem. Let the input array be arr[] of length n. We need to construct two arrays lis[] and lds[] using Dynamic Programming solution of LIS problem. lis[i] stores the length of the Longest Increasing subsequence ending with arr[i]. lds[i] stores the length of the longest Decreasing subsequence starting from arr[i]. Finally, we need to return the max value of lis[i] + lds[i] – 1 where i is from 0 to n-1.

[ad type=”banner”]

Following is Python implementation of the above Dynamic Programming solution.

[pastacode lang=”python” manual=”%23%20Dynamic%20Programming%20implementation%20of%20longest%20bitonic%20subsequence%20problem%0A%22%22%22%0Albs()%20returns%20the%20length%20of%20the%20Longest%20Bitonic%20Subsequence%20in%0Aarr%5B%5D%20of%20size%20n.%20The%20function%20mainly%20creates%20two%20temporary%20arrays%0Alis%5B%5D%20and%20lds%5B%5D%20and%20returns%20the%20maximum%20lis%5Bi%5D%20%2B%20lds%5Bi%5D%20-%201.%0A%20%0Alis%5Bi%5D%20%3D%3D%3E%20Longest%20Increasing%20subsequence%20ending%20with%20arr%5Bi%5D%0Alds%5Bi%5D%20%3D%3D%3E%20Longest%20decreasing%20subsequence%20starting%20with%20arr%5Bi%5D%0A%22%22%22%0A%20%0Adef%20lbs(arr)%3A%0A%20%20%20%20n%20%3D%20len(arr)%0A%20%0A%20%0A%20%20%20%20%23%20allocate%20memory%20for%20LIS%5B%5D%20and%20initialize%20LIS%20values%20as%201%0A%20%20%20%20%23%20for%20all%20indexes%0A%20%20%20%20lis%20%3D%20%5B1%20for%20i%20in%20range(n%2B1)%5D%0A%20%0A%20%20%20%20%23%20Compute%20LIS%20values%20from%20left%20to%20right%0A%20%20%20%20for%20i%20in%20range(1%20%2C%20n)%3A%0A%20%20%20%20%20%20%20%20for%20j%20in%20range(0%20%2C%20i)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20if%20((arr%5Bi%5D%20%3E%20arr%5Bj%5D)%20and%20(lis%5Bi%5D%20%3C%20lis%5Bj%5D%20%2B1))%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lis%5Bi%5D%20%3D%20lis%5Bj%5D%20%2B%201%0A%20%0A%20%20%20%20%23%20allocate%20memory%20for%20LDS%20and%20initialize%20LDS%20values%20for%0A%20%20%20%20%23%20all%20indexes%0A%20%20%20%20lds%20%3D%20%5B1%20for%20i%20in%20range(n%2B1)%5D%0A%20%20%20%20%20%0A%20%20%20%20%23%20Compute%20LDS%20values%20from%20right%20to%20left%0A%20%20%20%20for%20i%20in%20reversed(range(n-1))%3A%20%23loop%20from%20n-2%20downto%200%0A%20%20%20%20%20%20%20%20for%20j%20in%20reversed(range(i-1%20%2Cn))%3A%20%23loop%20from%20n-1%20downto%20i-1%0A%20%20%20%20%20%20%20%20%20%20%20%20if(arr%5Bi%5D%20%3E%20arr%5Bj%5D%20and%20lds%5Bi%5D%20%3C%20lds%5Bj%5D%20%2B%201)%3A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20lds%5Bi%5D%20%3D%20lds%5Bj%5D%20%2B%201%0A%20%0A%20%0A%20%20%20%20%23%20Return%20the%20maximum%20value%20of%20(lis%5Bi%5D%20%2B%20lds%5Bi%5D%20-%201)%0A%20%20%20%20maximum%20%3D%20lis%5B0%5D%20%2B%20lds%5B0%5D%20-%201%0A%20%20%20%20for%20i%20in%20range(1%20%2C%20n)%3A%0A%20%20%20%20%20%20%20%20maximum%20%3D%20max((lis%5Bi%5D%20%2B%20lds%5Bi%5D-1)%2C%20maximum)%0A%20%20%20%20%20%0A%20%20%20%20return%20maximum%0A%20%0A%23%20Driver%20program%20to%20test%20the%20above%20function%0Aarr%20%3D%20%20%5B0%20%2C%208%20%2C%204%2C%2012%2C%202%2C%2010%20%2C%206%20%2C%2014%20%2C%201%20%2C%209%20%2C%205%20%2C%2013%2C%0A%20%20%20%20%20%20%20%203%2C%2011%20%2C%207%20%2C%2015%5D%0Aprint%20%22Length%20of%20LBS%20is%22%2Clbs(arr)” message=”Python” highlight=”” provider=”manual”/]

Output :

 Length of LBS is 7

Time Complexity: O(n^2)
Auxiliary Space: O(n)

[ad type=”banner”]