Last Updated: 2 Jan, 2021

Longest Bitonic Sequence

Moderate
Asked in companies
AmazonMicrosoftWells Fargo

Problem statement

A Bitonic Sequence is a sequence of numbers that is first strictly increasing and then strictly decreasing.


A strictly ascending order sequence is also considered bitonic, with the decreasing part as empty, and same for a strictly descending order sequence.


For example, the sequences [1, 3, 5, 3, 2], [1, 2, 3, 4] are bitonic, whereas the sequences [5, 4, 1, 4, 5] and [1, 2, 2, 3] are not.


You are given an array 'arr' consisting of 'n' positive integers.


Find the length of the longest bitonic subsequence of 'arr'.


Example :
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.
Input Format :
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.


Output Format :
The output contains an integer, denoting the length of the longest bitonic subsequence.


Note:
You don’t need to print anything; it has already been taken care of. Just implement the given function.

Approaches

01 Approach

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 ‘longestIncreasingSubsequence’ 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 ‘longestDecreasingSubsequence’ 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.

02 Approach

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:

  1. We will call a recursion with the current index ‘currIndex’, the last index ‘last’ included in our subsequence, and a boolean variable ‘check’ denoting whether it is the increasing or decreasing part of the bitonic sequence as our parameters.
  2. In each recursive call:
    • We will call the recursion without including ‘arr[currIndex]’ in our subsequence.
    • If (‘check’ == 0), it denotes that we are in the increasing part of the bitonic sequence, so we check if ‘arr[currIndex]’ > ‘arr[last]’. If it is true we include it in our subsequence and call the recursion.
    • If (‘check’ == 1), it denotes that we are in the decreasing part of the bitonic sequence, so we check if ‘arr[currIndex]’ < ‘arr[last]’. If it is true we include it in our subsequence and call the recursion.

03 Approach

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:

  1. We will declare array/list ‘lis’ and ‘lds’ which store the length of the longest increasing subsequence ending at index ‘i’, and the length of the longest decreasing subsequence beginning at index ‘i’, respectively.
  2. For computing ‘lis[i]’ we will-
    • Iterate over all ‘j’ which are less than ‘i’.
    • If ‘arr[j]’ < ‘arr[i]’ we update ‘lis[i]’ as max(‘lis[i]’, ‘lis[j]’ + 1).
  3. For computing ‘LDS[i]’ we will-
    • Iterate over all ‘j’ which are greater than ‘i’.
    • If ‘arr[j]’ > ‘arr[i]’ we update ‘lds[i]’ as max(‘lds[i]’, ‘lds[j]’ + 1).
  4. Finally, we will iterate through all indices ‘i’, and compute the length of the longest bitonic sequence containing index ‘i’, as (‘lds[i]’ + ‘lis[i]’ - 1) and return the maximum as our answer.