Printing Longest Bitonic Subsequence
Last Updated :
16 Feb, 2023
The Longest Bitonic Subsequence problem is to find the longest subsequence of a given sequence such that it is first increasing and then decreasing. 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: [1, 11, 2, 10, 4, 5, 2, 1]
Output: [1, 2, 10, 4, 2, 1] OR [1, 11, 10, 5, 2, 1]
OR [1, 2, 4, 5, 2, 1]
Input: [12, 11, 40, 5, 3, 1]
Output: [12, 11, 5, 3, 1] OR [12, 40, 5, 3, 1]
Input: [80, 60, 30, 40, 20, 10]
Output: [80, 60, 30, 20, 10] OR [80, 60, 40, 20, 10]
In previous post, we have discussed about Longest Bitonic Subsequence problem. However, the post only covered code related to finding maximum sum of increasing subsequence, but not to the construction of subsequence. In this post, we will discuss how to construct Longest Bitonic Subsequence itself. Let arr[0..n-1] be the input array. We define vector LIS such that LIS[i] is itself is a vector that stores Longest Increasing Subsequence of arr[0..i] that ends with arr[i]. Therefore for an index i, LIS[i] can be recursively written as -
LIS[0] = {arr[O]}
LIS[i] = {Max(LIS[j])} + arr[i] where j < i and arr[j] < arr[i]
= arr[i], if there is no such j
We also define a vector LDS such that LDS[i] is itself is a vector that stores Longest Decreasing Subsequence of arr[i..n] that starts with arr[i]. Therefore for an index i, LDS[i] can be recursively written as -
LDS[n] = {arr[n]}
LDS[i] = arr[i] + {Max(LDS[j])} where j > i and arr[j] < arr[i]
= arr[i], if there is no such j
For example, for array [1 11 2 10 4 5 2 1],
LIS[0]: 1
LIS[1]: 1 11
LIS[2]: 1 2
LIS[3]: 1 2 10
LIS[4]: 1 2 4
LIS[5]: 1 2 4 5
LIS[6]: 1 2
LIS[7]: 1
LDS[0]: 1
LDS[1]: 11 10 5 2 1
LDS[2]: 2 1
LDS[3]: 10 5 2 1
LDS[4]: 4 2 1
LDS[5]: 5 2 1
LDS[6]: 2 1
LDS[7]: 1
Therefore, Longest Bitonic Subsequence can be
LIS[1] + LDS[1] = [1 11 10 5 2 1] OR
LIS[3] + LDS[3] = [1 2 10 5 2 1] OR
LIS[5] + LDS[5] = [1 2 4 5 2 1]
Below is the implementation of above idea –
C++
/* Dynamic Programming solution to print Longest
Bitonic Subsequence */
#include <bits/stdc++.h>
using namespace std;
// Utility function to print Longest Bitonic
// Subsequence
void print(vector<int>& arr, int size)
{
for(int i = 0; i < size; i++)
cout << arr[i] << " ";
}
// Function to construct and print Longest
// Bitonic Subsequence
void printLBS(int arr[], int n)
{
// LIS[i] stores the length of the longest
// increasing subsequence ending with arr[i]
vector<vector<int>> LIS(n);
// initialize LIS[0] to arr[0]
LIS[0].push_back(arr[0]);
// Compute LIS values from left to right
for (int i = 1; i < n; i++)
{
// for every j less than i
for (int j = 0; j < i; j++)
{
if ((arr[j] < arr[i]) &&
(LIS[j].size() > LIS[i].size()))
LIS[i] = LIS[j];
}
LIS[i].push_back(arr[i]);
}
/* LIS[i] now stores Maximum Increasing
Subsequence of arr[0..i] that ends with
arr[i] */
// LDS[i] stores the length of the longest
// decreasing subsequence starting with arr[i]
vector<vector<int>> LDS(n);
// initialize LDS[n-1] to arr[n-1]
LDS[n - 1].push_back(arr[n - 1]);
// Compute LDS values from right to left
for (int i = n - 2; i >= 0; i--)
{
// for every j greater than i
for (int j = n - 1; j > i; j--)
{
if ((arr[j] < arr[i]) &&
(LDS[j].size() > LDS[i].size()))
LDS[i] = LDS[j];
}
LDS[i].push_back(arr[i]);
}
// reverse as vector as we're inserting at end
for (int i = 0; i < n; i++)
reverse(LDS[i].begin(), LDS[i].end());
/* LDS[i] now stores Maximum Decreasing Subsequence
of arr[i..n] that starts with arr[i] */
int max = 0;
int maxIndex = -1;
for (int i = 0; i < n; i++)
{
// Find maximum value of size of LIS[i] + size
// of LDS[i] - 1
if (LIS[i].size() + LDS[i].size() - 1 > max)
{
max = LIS[i].size() + LDS[i].size() - 1;
maxIndex = i;
}
}
// print all but last element of LIS[maxIndex] vector
print(LIS[maxIndex], LIS[maxIndex].size() - 1);
// print all elements of LDS[maxIndex] vector
print(LDS[maxIndex], LDS[maxIndex].size());
}
// Driver program
int main()
{
int arr[] = { 1, 11, 2, 10, 4, 5, 2, 1 };
int n = sizeof(arr) / sizeof(arr[0]);
printLBS(arr, n);
return 0;
}
Java
/* Dynamic Programming solution to print Longest
Bitonic Subsequence */
import java.util.*;
class GFG
{
// Utility function to print Longest Bitonic
// Subsequence
static void print(Vector<Integer> arr, int size)
{
for (int i = 0; i < size; i++)
System.out.print(arr.elementAt(i) + " ");
}
// Function to construct and print Longest
// Bitonic Subsequence
static void printLBS(int[] arr, int n)
{
// LIS[i] stores the length of the longest
// increasing subsequence ending with arr[i]
@SuppressWarnings("unchecked")
Vector<Integer>[] LIS = new Vector[n];
for (int i = 0; i < n; i++)
LIS[i] = new Vector<>();
// initialize LIS[0] to arr[0]
LIS[0].add(arr[0]);
// Compute LIS values from left to right
for (int i = 1; i < n; i++)
{
// for every j less than i
for (int j = 0; j < i; j++)
{
if ((arr[i] > arr[j]) &&
LIS[j].size() > LIS[i].size())
{
for (int k : LIS[j])
if (!LIS[i].contains(k))
LIS[i].add(k);
}
}
LIS[i].add(arr[i]);
}
/*
* LIS[i] now stores Maximum Increasing Subsequence
* of arr[0..i] that ends with arr[i]
*/
// LDS[i] stores the length of the longest
// decreasing subsequence starting with arr[i]
@SuppressWarnings("unchecked")
Vector<Integer>[] LDS = new Vector[n];
for (int i = 0; i < n; i++)
LDS[i] = new Vector<>();
// initialize LDS[n-1] to arr[n-1]
LDS[n - 1].add(arr[n - 1]);
// Compute LDS values from right to left
for (int i = n - 2; i >= 0; i--)
{
// for every j greater than i
for (int j = n - 1; j > i; j--)
{
if (arr[j] < arr[i] &&
LDS[j].size() > LDS[i].size())
for (int k : LDS[j])
if (!LDS[i].contains(k))
LDS[i].add(k);
}
LDS[i].add(arr[i]);
}
// reverse as vector as we're inserting at end
for (int i = 0; i < n; i++)
Collections.reverse(LDS[i]);
/*
* LDS[i] now stores Maximum Decreasing Subsequence
* of arr[i..n] that starts with arr[i]
*/
int max = 0;
int maxIndex = -1;
for (int i = 0; i < n; i++)
{
// Find maximum value of size of
// LIS[i] + size of LDS[i] - 1
if (LIS[i].size() + LDS[i].size() - 1 > max)
{
max = LIS[i].size() + LDS[i].size() - 1;
maxIndex = i;
}
}
// print all but last element of LIS[maxIndex] vector
print(LIS[maxIndex], LIS[maxIndex].size() - 1);
// print all elements of LDS[maxIndex] vector
print(LDS[maxIndex], LDS[maxIndex].size());
}
// Driver Code
public static void main(String[] args)
{
int[] arr = { 1, 11, 2, 10, 4, 5, 2, 1 };
int n = arr.length;
printLBS(arr, n);
}
}
// This code is contributed by
// sanjeev2552
Python3
# Dynamic Programming solution to print Longest
# Bitonic Subsequence
def _print(arr: list, size: int):
for i in range(size):
print(arr[i], end=" ")
# Function to construct and print Longest
# Bitonic Subsequence
def printLBS(arr: list, n: int):
# LIS[i] stores the length of the longest
# increasing subsequence ending with arr[i]
LIS = [0] * n
for i in range(n):
LIS[i] = []
# initialize LIS[0] to arr[0]
LIS[0].append(arr[0])
# Compute LIS values from left to right
for i in range(1, n):
# for every j less than i
for j in range(i):
if ((arr[j] < arr[i]) and (len(LIS[j]) > len(LIS[i]))):
LIS[i] = LIS[j].copy()
LIS[i].append(arr[i])
# LIS[i] now stores Maximum Increasing
# Subsequence of arr[0..i] that ends with
# arr[i]
# LDS[i] stores the length of the longest
# decreasing subsequence starting with arr[i]
LDS = [0] * n
for i in range(n):
LDS[i] = []
# initialize LDS[n-1] to arr[n-1]
LDS[n - 1].append(arr[n - 1])
# Compute LDS values from right to left
for i in range(n - 2, -1, -1):
# for every j greater than i
for j in range(n - 1, i, -1):
if ((arr[j] < arr[i]) and (len(LDS[j]) > len(LDS[i]))):
LDS[i] = LDS[j].copy()
LDS[i].append(arr[i])
# reverse as vector as we're inserting at end
for i in range(n):
LDS[i] = list(reversed(LDS[i]))
# LDS[i] now stores Maximum Decreasing Subsequence
# of arr[i..n] that starts with arr[i]
max = 0
maxIndex = -1
for i in range(n):
# Find maximum value of size of LIS[i] + size
# of LDS[i] - 1
if (len(LIS[i]) + len(LDS[i]) - 1 > max):
max = len(LIS[i]) + len(LDS[i]) - 1
maxIndex = i
# print all but last element of LIS[maxIndex] vector
_print(LIS[maxIndex], len(LIS[maxIndex]) - 1)
# print all elements of LDS[maxIndex] vector
_print(LDS[maxIndex], len(LDS[maxIndex]))
# Driver Code
if __name__ == "__main__":
arr = [1, 11, 2, 10, 4, 5, 2, 1]
n = len(arr)
printLBS(arr, n)
# This code is contributed by
# sanjeev2552
C#
/* Dynamic Programming solution to print longest
Bitonic Subsequence */
using System;
using System.Linq;
using System.Collections.Generic;
class GFG
{
// Utility function to print longest Bitonic
// Subsequence
static void print(List<int> arr, int size)
{
for (int i = 0; i < size; i++)
Console.Write(arr[i] + " ");
}
// Function to construct and print longest
// Bitonic Subsequence
static void printLBS(int[] arr, int n)
{
// LIS[i] stores the length of the longest
// increasing subsequence ending with arr[i]
List<int>[] LIS = new List<int>[n];
for (int i = 0; i < n; i++)
LIS[i] = new List<int>();
// initialize LIS[0] to arr[0]
LIS[0].Add(arr[0]);
// Compute LIS values from left to right
for (int i = 1; i < n; i++)
{
// for every j less than i
for (int j = 0; j < i; j++)
{
if ((arr[i] > arr[j]) &&
LIS[j].Count > LIS[i].Count)
{
foreach (int k in LIS[j])
if (!LIS[i].Contains(k))
LIS[i].Add(k);
}
}
LIS[i].Add(arr[i]);
}
/*
* LIS[i] now stores Maximum Increasing Subsequence
* of arr[0..i] that ends with arr[i]
*/
// LDS[i] stores the length of the longest
// decreasing subsequence starting with arr[i]
List<int>[] LDS = new List<int>[n];
for (int i = 0; i < n; i++)
LDS[i] = new List<int>();
// initialize LDS[n-1] to arr[n-1]
LDS[n - 1].Add(arr[n - 1]);
// Compute LDS values from right to left
for (int i = n - 2; i >= 0; i--)
{
// for every j greater than i
for (int j = n - 1; j > i; j--)
{
if (arr[j] < arr[i] &&
LDS[j].Count > LDS[i].Count)
foreach (int k in LDS[j])
if (!LDS[i].Contains(k))
LDS[i].Add(k);
}
LDS[i].Add(arr[i]);
}
// reverse as vector as we're inserting at end
for (int i = 0; i < n; i++)
LDS[i].Reverse();
/*
* LDS[i] now stores Maximum Decreasing Subsequence
* of arr[i..n] that starts with arr[i]
*/
int max = 0;
int maxIndex = -1;
for (int i = 0; i < n; i++)
{
// Find maximum value of size of
// LIS[i] + size of LDS[i] - 1
if (LIS[i].Count + LDS[i].Count - 1 > max)
{
max = LIS[i].Count + LDS[i].Count - 1;
maxIndex = i;
}
}
// print all but last element of LIS[maxIndex] vector
print(LIS[maxIndex], LIS[maxIndex].Count - 1);
// print all elements of LDS[maxIndex] vector
print(LDS[maxIndex], LDS[maxIndex].Count);
}
// Driver Code
public static void Main(String[] args)
{
int[] arr = { 1, 11, 2, 10, 4, 5, 2, 1 };
int n = arr.Length;
printLBS(arr, n);
}
}
// This code is contributed by PrinciRaj1992
JavaScript
// Function to print the longest bitonic subsequence
function _print(arr, size) {
for (let i = 0; i<size; i++) {
process.stdout.write(arr[i]+' ');
}
}
// Function to construct and print the longest bitonic subsequence
function printLBS(arr, n) {
// LIS[i] stores the length of the longest increasing subsequence ending with arr[i]
let LIS = new Array(n);
for (let i = 0; i < n; i++) {
LIS[i] = [];
}
// initialize LIS[0] to arr[0]
LIS[0].push(arr[0]);
// Compute LIS values from left to right
for (let i = 1; i < n; i++) {
// for every j less than i
for (let j = 0; j < i; j++) {
if (arr[j] < arr[i] && LIS[j].length > LIS[i].length) {
LIS[i] = LIS[j].slice();
}
}
LIS[i].push(arr[i]);
}
// LIS[i] now stores the Maximum Increasing Subsequence of arr[0..i] that ends with arr[i]
// LDS[i] stores the length of the longest decreasing subsequence starting with arr[i]
let LDS = new Array(n);
for (let i = 0; i < n; i++) {
LDS[i] = [];
}
// initialize LDS[n-1] to arr[n-1]
LDS[n - 1].push(arr[n - 1]);
// Compute LDS values from right to left
for (let i = n - 2; i >= 0; i--) {
// for every j greater than i
for (let j = n - 1; j > i; j--) {
if (arr[j] < arr[i] && LDS[j].length > LDS[i].length) {
LDS[i] = LDS[j].slice();
}
}
LDS[i].push(arr[i]);
}
// reverse the LDS vector as we're inserting at the end
for (let i = 0; i < n; i++) {
LDS[i].reverse();
}
// LDS[i] now stores the Maximum Decreasing Subsequence of arr[i..n] that starts with arr[i]
let max = 0;
let maxIndex = -1;
for (let i = 0; i < n; i++) {
// Find maximum value of size of LIS[i] + size of LDS[i] - 1
if (LIS[i].length + LDS[i].length - 1 > max) {
max = LIS[i].length + LDS[i].length - 1;
maxIndex = i;
}
}
// print all but
// print all but last element of LIS[maxIndex] array
_print(LIS[maxIndex].slice(0, -1), LIS[maxIndex].length - 1);
// print all elements of LDS[maxIndex] array
_print(LDS[maxIndex], LDS[maxIndex].length);
}
// Driver program
const arr = [1, 11, 2, 10, 4, 5, 2, 1];
const n = arr.length;
printLBS(arr, n);
Output:
1 11 10 5 2 1
Time complexity of above Dynamic Programming solution is O(n2). Auxiliary space used by the program is O(n2).
Similar Reads
Basics & Prerequisites
Data Structures
Array Data StructureIn this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
3 min read
String in Data StructureA string is a sequence of characters. The following facts make string an interesting data structure.Small set of elements. Unlike normal array, strings typically have smaller set of items. For example, lowercase English alphabet has only 26 characters. ASCII has only 256 characters.Strings are immut
2 min read
Hashing in Data StructureHashing is a technique used in data structures that efficiently stores and retrieves data in a way that allows for quick access. Hashing involves mapping data to a specific index in a hash table (an array of items) using a hash function. It enables fast retrieval of information based on its key. The
2 min read
Linked List Data StructureA linked list is a fundamental data structure in computer science. It mainly allows efficient insertion and deletion operations compared to arrays. Like arrays, it is also used to implement other data structures like stack, queue and deque. Hereâs the comparison of Linked List vs Arrays Linked List:
2 min read
Stack Data StructureA Stack is a linear data structure that follows a particular order in which the operations are performed. The order may be LIFO(Last In First Out) or FILO(First In Last Out). LIFO implies that the element that is inserted last, comes out first and FILO implies that the element that is inserted first
2 min read
Queue Data StructureA Queue Data Structure is a fundamental concept in computer science used for storing and managing data in a specific order. It follows the principle of "First in, First out" (FIFO), where the first element added to the queue is the first one to be removed. It is used as a buffer in computer systems
2 min read
Tree Data StructureTree Data Structure is a non-linear data structure in which a collection of elements known as nodes are connected to each other via edges such that there exists exactly one path between any two nodes. Types of TreeBinary Tree : Every node has at most two childrenTernary Tree : Every node has at most
4 min read
Graph Data StructureGraph Data Structure is a collection of nodes connected by edges. It's used to represent relationships between different entities. If you are looking for topic-wise list of problems on different topics like DFS, BFS, Topological Sort, Shortest Path, etc., please refer to Graph Algorithms. Basics of
3 min read
Trie Data StructureThe Trie data structure is a tree-like structure used for storing a dynamic set of strings. It allows for efficient retrieval and storage of keys, making it highly effective in handling large datasets. Trie supports operations such as insertion, search, deletion of keys, and prefix searches. In this
15+ min read
Algorithms
Searching AlgorithmsSearching algorithms are essential tools in computer science used to locate specific items within a collection of data. In this tutorial, we are mainly going to focus upon searching in an array. When we search an item in an array, there are two most common algorithms used based on the type of input
2 min read
Sorting AlgorithmsA Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read
Introduction to RecursionThe process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
Greedy AlgorithmsGreedy algorithms are a class of algorithms that make locally optimal choices at each step with the hope of finding a global optimum solution. At every step of the algorithm, we make a choice that looks the best at the moment. To make the choice, we sometimes sort the array so that we can always get
3 min read
Graph AlgorithmsGraph is a non-linear data structure like tree data structure. The limitation of tree is, it can only represent hierarchical data. For situations where nodes or vertices are randomly connected with each other other, we use Graph. Example situations where we use graph data structure are, a social net
3 min read
Dynamic Programming or DPDynamic Programming is an algorithmic technique with the following properties.It is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming. The idea is to simply store the results of
3 min read
Bitwise AlgorithmsBitwise algorithms in Data Structures and Algorithms (DSA) involve manipulating individual bits of binary representations of numbers to perform operations efficiently. These algorithms utilize bitwise operators like AND, OR, XOR, NOT, Left Shift, and Right Shift.BasicsIntroduction to Bitwise Algorit
4 min read
Advanced
Segment TreeSegment Tree is a data structure that allows efficient querying and updating of intervals or segments of an array. It is particularly useful for problems involving range queries, such as finding the sum, minimum, maximum, or any other operation over a specific range of elements in an array. The tree
3 min read
Pattern SearchingPattern searching algorithms are essential tools in computer science and data processing. These algorithms are designed to efficiently find a particular pattern within a larger set of data. Patten SearchingImportant Pattern Searching Algorithms:Naive String Matching : A Simple Algorithm that works i
2 min read
GeometryGeometry is a branch of mathematics that studies the properties, measurements, and relationships of points, lines, angles, surfaces, and solids. From basic lines and angles to complex structures, it helps us understand the world around us.Geometry for Students and BeginnersThis section covers key br
2 min read
Interview Preparation
Practice Problem