Practice › Dynamic Programming
Longest Bitonic Subsequence
Medium · Dynamic Programming · Asked at Amazon, Microsoft, Samsung
A subsequence is bitonic if it first strictly increases and then strictly decreases. Either part may be empty, so a strictly increasing or strictly decreasing subsequence also counts. Print the length of the longest bitonic subsequence.
Input
First line: n. Second line: n space-separated integers.
Output
The length of the longest bitonic subsequence.
Constraints
- 1 ≤ n ≤ 2000
- −10^9 ≤ a[i] ≤ 10^9
Compute inc[i] = longest strictly increasing subsequence ending at i (left to right) and dec[i] = longest strictly decreasing subsequence starting at i (right to left). The answer is max(inc[i] + dec[i] − 1).
Example 1
Input
8 1 11 2 10 4 5 2 1
Output
6
Example 2
Input
6 12 11 40 5 3 1
Output
5
Hints
- A bitonic subsequence has a peak element.
- Left of the peak is an increasing subsequence ending at it; right of it a decreasing one starting at it.
- Compute both for every index, then combine.
Approach
inc[i] = 1 + max(inc[j]) over j < i with a[j] < a[i] — the classic O(n²) LIS. dec[i] is the same from the right: 1 + max(dec[j]) over j > i with a[j] < a[i]. Each index as the peak gives inc[i] + dec[i] − 1 (the peak is counted twice). Take the maximum. Because both parts may be empty, a single element already gives 1.
Complexity: O(n²) time · O(n) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<long long> a(n);
for (auto &x : a) cin >> x;
vector<int> inc(n, 1), dec(n, 1);
for (int i = 0; i < n; i++)
for (int j = 0; j < i; j++)
if (a[j] < a[i]) inc[i] = max(inc[i], inc[j] + 1);
for (int i = n - 1; i >= 0; i--)
for (int j = n - 1; j > i; j--)
if (a[j] < a[i]) dec[i] = max(dec[i], dec[j] + 1);
int best = 0;
for (int i = 0; i < n; i++) best = max(best, inc[i] + dec[i] - 1);
cout << best << "\n";
return 0;
}Python solution
import sys
data = list(map(int, sys.stdin.read().split()))
n = data[0]
a = data[1 : 1 + n]
inc = [1] * n
dec = [1] * n
for i in range(n):
ai = a[i]
best = 0
for j in range(i):
if a[j] < ai and inc[j] > best:
best = inc[j]
inc[i] = best + 1
for i in range(n - 1, -1, -1):
ai = a[i]
best = 0
for j in range(i + 1, n):
if a[j] < ai and dec[j] > best:
best = dec[j]
dec[i] = best + 1
print(max(inc[i] + dec[i] - 1 for i in range(n)))Java solution
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
long[] a = new long[n];
for (int i = 0; i < n; i++) {
in.nextToken();
a[i] = (long) in.nval;
}
int[] inc = new int[n], dec = new int[n];
for (int i = 0; i < n; i++) {
inc[i] = 1;
for (int j = 0; j < i; j++) if (a[j] < a[i]) inc[i] = Math.max(inc[i], inc[j] + 1);
}
for (int i = n - 1; i >= 0; i--) {
dec[i] = 1;
for (int j = n - 1; j > i; j--) if (a[j] < a[i]) dec[i] = Math.max(dec[i], dec[j] + 1);
}
int best = 0;
for (int i = 0; i < n; i++) best = Math.max(best, inc[i] + dec[i] - 1);
System.out.println(best);
}
}Solve Longest Bitonic Subsequence online · All practice problems