Longest Bitonic Subsequence | DP-15
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)
Source: Microsoft Interview Question
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.
Following is the implementation of the above Dynamic Programming solution.
C++
#include<stdio.h>
#include<stdlib.h>
int lbs( int arr[], int n )
{
int i, j;
int lis[n];
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
int lds[n];
for (i = 0; i < n; i++)
lds[i] = 1;
for (i = n-2; i >= 0; i--)
for (j = n-1; j > i; j--)
if (arr[i] > arr[j] && lds[i] < lds[j] + 1)
lds[i] = lds[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" , lbs( arr, n ) );
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
import java.io.*;
class LBS
{
static int lbs( int arr[], int n )
{
int i, j;
int [] lis = new int [n];
for (i = 0 ; i < n; i++)
lis[i] = 1 ;
for (i = 1 ; i < n; i++)
for (j = 0 ; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1 )
lis[i] = lis[j] + 1 ;
int [] lds = new int [n];
for (i = 0 ; i < n; i++)
lds[i] = 1 ;
for (i = n- 2 ; i >= 0 ; i--)
for (j = n- 1 ; j > i; j--)
if (arr[i] > arr[j] && lds[i] < lds[j] + 1 )
lds[i] = lds[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 " + lbs( arr, n ));
}
}
|
Python3
def lbs(arr):
n = len (arr)
lis = [ 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 (lis[i] < lis[j] + 1 )):
lis[i] = lis[j] + 1
lds = [ 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 lds[i] < lds[j] + 1 ):
lds[i] = lds[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" ,lbs(arr))
|
C#
using System;
class LBS {
static int lbs( int [] arr, int n)
{
int i, j;
int [] lis = new int [n];
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
int [] lds = new int [n];
for (i = 0; i < n; i++)
lds[i] = 1;
for (i = n - 2; i >= 0; i--)
for (j = n - 1; j > i; j--)
if (arr[i] > arr[j] && lds[i] < lds[j] + 1)
lds[i] = lds[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 = { 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 " + lbs(arr, n));
}
}
|
PHP
<?php
function lbs(& $arr , $n )
{
$lis = array_fill (0, $n , NULL);
for ( $i = 0; $i < $n ; $i ++)
$lis [ $i ] = 1;
for ( $i = 1; $i < $n ; $i ++)
for ( $j = 0; $j < $i ; $j ++)
if ( $arr [ $i ] > $arr [ $j ] &&
$lis [ $i ] < $lis [ $j ] + 1)
$lis [ $i ] = $lis [ $j ] + 1;
$lds = array_fill (0, $n , NULL);
for ( $i = 0; $i < $n ; $i ++)
$lds [ $i ] = 1;
for ( $i = $n - 2; $i >= 0; $i --)
for ( $j = $n - 1; $j > $i ; $j --)
if ( $arr [ $i ] > $arr [ $j ] &&
$lds [ $i ] < $lds [ $j ] + 1)
$lds [ $i ] = $lds [ $j ] + 1;
$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 ;
}
$arr = array (0, 8, 4, 12, 2, 10, 6, 14,
1, 9, 5, 13, 3, 11, 7, 15);
$n = sizeof( $arr );
echo "Length of LBS is " . lbs( $arr , $n );
?>
|
Javascript
<script>
function lbs(arr,n)
{
let i, j;
let lis = new Array(n)
for (i = 0; i < n; i++)
lis[i] = 1;
for (i = 1; i < n; i++)
for (j = 0; j < i; j++)
if (arr[i] > arr[j] && lis[i] < lis[j] + 1)
lis[i] = lis[j] + 1;
let lds = new Array(n);
for (i = 0; i < n; i++)
lds[i] = 1;
for (i = n-2; i >= 0; i--)
for (j = n-1; j > i; j--)
if (arr[i] > arr[j] && lds[i] < lds[j] + 1)
lds[i] = lds[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 " + lbs( arr, n ));
</script>
|
Output
Length of LBS is 7
Time Complexity: O(n^2)
Auxiliary Space: O(n)
Last Updated :
20 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...