Length of longest strict bitonic subsequence

Given an array arr[] containing n integers. The problem is to find the length of the longest strict bitonic subsequence. A subsequence is called strict bitonic if it is first increasing and then decreasing with the condition that in both the increasing and decreasing parts the absolute difference between adjacents is 1 only. 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, 5, 2, 3, 4, 5, 3, 2}
Output : 6
The Longest Strict Bitonic Subsequence is:
{1, 2, 3, 4, 3, 2}.

Input : arr[] = {1, 2, 5, 3, 6, 7, 4, 6, 5}
Output : 5

Method 1: The problem could be solved using the concept of finding the longest bitonic subsequence. The only condition that needs to be maintained is that the adjacents should have a difference of 1 only. It has a time complexity of O(n2).

Method 2 (Efficient Approach): The idea is to create two hash maps inc and dcr having tuples in the form (ele, len), where len denotes the length of the longest increasing subsequence ending with the element ele in map inc and length of the longest decreasing subsequence starting with element ele in map dcr respectively. Also create two arrays len_inc[] and len_dcr[] where len_inc[i] represents the length of the largest increasing subsequence ending with element arr[i] and len_dcr[i] represents the length of the largest decreasing subsequence starting with element arr[i]. Now, for each element arr[i] we can find the length of the value (arr[i]-1) if it exists in the hash table inc. Let this value be v (initially v will be 0). Now, the length of longest increasing subsequence ending with arr[i] would be v+1. Update this length along with the element arr[i] in the hash table inc and in the array len_inc[] at respective index i. Now, traversing the array from right to left we can similarly fill the hash table dcr and array len_dcr[] for longest decreasing subsequence. Finally, for each element arr[i] we calculate (len_inc[i] + len_dcr[i] – 1) and return the maximum value.

Note: Here increasing and decreasing subsequences only mean that the difference between adjacent elements is 1 only.

// C++ implementation to find length of longest 
// strict bitonic subsequence 
#include <bits/stdc++.h>
using namespace std;
   
// function to find length of longest 
// strict bitonic subsequence 
int longLenStrictBitonicSub(int arr[], int n)
{
    // hash table to map the array element with the
    // length of the longest subsequence of which
    // it is a part of and is the last/first element of
    // that subsequence
    unordered_map<int, int> inc, dcr;
    
    // arrays to store the length of increasing and
    // decreasing subsequences which end at them
    // or start from them  
    int len_inc[n], len_dcr[n];
    
    // to store the length of longest strict 
    // bitonic subsequence
    int longLen = 0;
    
    // traverse the array elements
    // from left to right
    for (int i=0; i<n; i++)
    {
        // initialize current length 
        // for element arr[i] as 0
        int len = 0;
          
        // if 'arr[i]-1' is in 'inc'
        if (inc.find(arr[i]-1) != inc.end())
            len = inc[arr[i]-1];
          
        // update arr[i] subsequence length in 'inc'    
        // and in len_inc[]
        inc[arr[i]] = len_inc[i] = len + 1; 
    }
    
    // traverse the array elements
    // from right to left
    for (int i=n-1; i>=0; i--)
    {
        // initialize current length 
        // for element arr[i] as 0
        int len = 0;
          
        // if 'arr[i]-1' is in 'dcr' 
        if (dcr.find(arr[i]-1) != dcr.end())
            len = dcr[arr[i]-1];
          
        // update arr[i] subsequence length in 'dcr'
        // and in len_dcr[]    
        dcr[arr[i]] = len_dcr[i] = len + 1; 
    }
    
    // calculating the length of all the strict 
    // bitonic subsequence
    for (int i=0; i<n; i++)
        if (longLen < (len_inc[i] + len_dcr[i] - 1))
            longLen = len_inc[i] + len_dcr[i] - 1;
       
    // required longest length strict 
    // bitonic subsequence
    return longLen;        
}
   
// Driver program to test above
int main()
{
    int arr[] = {1, 5, 2, 3, 4, 5, 3, 2};
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << "Longest length strict bitonic subsequence = "
         << longLenStrictBitonicSub(arr, n);
    return 0;
}  

Output:

Longest length strict bitonic subsequence = 6

Time Complexity: O(n).
Auxiliary Space: O(n).

This article is contributed by ayushjauhari14. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



0 Average Difficulty : 0/5.0
No votes yet.










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.