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: 5
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++
#include <bits/stdc++.h>
using namespace std;
int ReverseBitonic( int arr[], int N)
{
int i, j;
int lds[N];
for (i = 0; i < N; i++) {
lds[i] = 1;
}
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;
}
}
}
int lis[N];
for (i = 0; i < N; i++) {
lis[i] = 1;
}
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;
}
}
}
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 max;
}
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;
}
|
Java
import java.io.*;
class GFG{
static int ReverseBitonic( int arr[], int N)
{
int i, j;
int [] lds = new int [N];
for (i = 0 ; i < N; i++)
{
lds[i] = 1 ;
}
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 ;
}
}
}
int [] lis = new int [N];
for (i = 0 ; i < N; i++)
{
lis[i] = 1 ;
}
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 ;
}
}
}
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 max;
}
public static void main (String[] args)
{
int arr[] = { 0 , 8 , 4 , 12 ,
2 , 10 , 6 , 14 ,
1 , 9 , 5 , 13 ,
3 , 11 , 7 , 15 };
int N = arr.length;
System.out.println( "Length of LBS is " +
ReverseBitonic(arr, N));
}
}
|
Python3
def ReverseBitonic(arr):
N = len (arr)
lds = [ 1 for i in range (N + 1 )]
for i in range ( 1 , N):
for j in range ( 0 , i):
if ((arr[i] < arr[j]) and
(lds[i] < lds[j] + 1 )):
lds[i] = lds[j] + 1
lis = [ 1 for i in range (N + 1 )]
for i in reversed ( range (N - 1 )):
for j in reversed ( range (i - 1 , N)):
if (arr[i] < arr[j] and
lis[i] < lis[j] + 1 ):
lis[i] = lis[j] + 1
maximum = lis[ 0 ] + lds[ 0 ] - 1
for i in range ( 1 , N):
maximum = max ((lis[i] +
lds[i] - 1 ), maximum)
return maximum
arr = [ 0 , 8 , 4 , 12 ,
2 , 10 , 6 , 14 ,
1 , 9 , 5 , 13 ,
3 , 11 , 7 , 15 ]
print ( "Length of LBS is" , ReverseBitonic(arr))
|
C#
using System;
class GFG{
static int ReverseBitonic( int [] arr, int N)
{
int i, j;
int [] lds = new int [N];
for (i = 0; i < N; i++)
{
lds[i] = 1;
}
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;
}
}
}
int [] lis = new int [N];
for (i = 0; i < N; i++)
{
lis[i] = 1;
}
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;
}
}
}
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 max;
}
public static void Main ()
{
int [] arr = new int [] { 0, 8, 4, 12,
2, 10, 6, 14,
1, 9, 5, 13,
3, 11, 7, 15 };
int N = arr.Length;
Console.WriteLine( "Length of LBS is " +
ReverseBitonic(arr, N));
}
}
|
Javascript
<script>
function ReverseBitonic(arr, N)
{
let i, j;
let lds = [];
for (i = 0; i < N; i++)
{
lds[i] = 1;
}
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;
}
}
}
let lis = [];
for (i = 0; i < N; i++)
{
lis[i] = 1;
}
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;
}
}
}
let 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 max;
}
let arr = [ 0, 8, 4, 12,
2, 10, 6, 14,
1, 9, 5, 13,
3, 11, 7, 15 ];
let N = arr.length;
document.write( "Length of LBS is " +
ReverseBitonic(arr, N));
</script>
|
Output:
Length of LBS is 7
Time Complexity: O(N2)
Auxiliary Space: O(N)
Last Updated :
19 Apr, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...