Longest Reverse Bitonic Sequence

Given an arr[] of length N, the task is to find the length of longest Reverse Bitonic Subsequence. A subsequence is called Reverse Bitonic if it is first decreasing, then increasing.

Examples:

Input: arr[] = {10, 11, 2, 1, 1, 5, 2, 4}
Output: 6
Explanation: The longest subsequence which first decreases than increases is {10, 2, 1, 1, 2, 4}
Input: arr[] = {0, 8, 4, 12, 2, 10, 6, 14, 1, 9, 5, 13, 3, 11, 7, 15}
Output: 7
Explanation: The longest subsequence which first decreases than increases is {12, 10, 6, 1, 9, 11, 15}

Approach:
This problem is a variation of standard Longest Increasing Subsequence (LIS) problem. Construct two arrays lis[] and lds[] to store the longest increasing and decreasing subsequences respectively upto every ith index of the array using Dynamic Programming. Finally, return the max value of lds[i] + lis[i] – 1 where i ranges from 0 to N-1.

Below is the implementation of the above approach:



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to find the
// longest Reverse bitonic
// Subsequence
#include <bits/stdc++.h>
using namespace std;
  
// Function to return the length
// of the Longest Reverse Bitonic
// Subsequence in the array
int ReverseBitonic(int arr[], int N)
{
    int i, j;
  
    // Allocate memory for LIS[] and
    // initialize LIS values as 1 for
    // all indexes
    int lds[N];
    for (i = 0; i < N; i++) {
        lds[i] = 1;
    }
  
    // Compute LIS values from left
    // to right for every index
    for (i = 1; i < N; i++) {
        for (j = 0; j < i; j++) {
            if (arr[i] < arr[j]
                && lds[i] < lds[j] + 1) {
                lds[i] = lds[j] + 1;
            }
        }
    }
  
    // Initialize LDS for
    // all indexes as 1
    int lis[N];
    for (i = 0; i < N; i++) {
        lis[i] = 1;
    }
  
    // Compute LDS values for every
    // index from right to left
    for (i = N - 2; i >= 0; i--) {
        for (j = N - 1; j > i; j--) {
            if (arr[i] < arr[j]
                && lis[i] < lis[j] + 1) {
                lis[i] = lis[j] + 1;
            }
        }
    }
  
    // Find the maximum value of
    // lis[i] + lds[i] - 1
    // in the array
    int max = lis[0] + lds[0] - 1;
    for (i = 1; i < N; i++) {
        if (lis[i] + lds[i] - 1 > max) {
            max = lis[i] + lds[i] - 1;
        }
    }
    // Return the maximum
    return max;
}
  
// Driver Program
int main()
{
  
    int arr[] = { 0, 8, 4, 12, 2, 10, 6,
                  14, 1, 9, 5, 13, 3, 11,
                  7, 15 };
    int N = sizeof(arr) / sizeof(arr[0]);
    printf("Length of LBS is %d\n",
           ReverseBitonic(arr, N));
    return 0;
}

chevron_right


Output:

Length of LBS is 7

Time Complexity: O(N2)
Auxiliary Space: O(N)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.