-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLongestBitonicSubsequence.java
51 lines (43 loc) · 1.19 KB
/
LongestBitonicSubsequence.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Given an array of positive integers. The task is to print the maximum length of Bitonic subsequence.
* A subsequenceof array is called Bitonic if it is first increasing, then decreasing.
*/
package DataStructuresAndAlgorithms.src.dynamicProgramming;
public class LongestBitonicSubsequence {
public static class biotonicSubsequence {
public static int lbs(int[] arr, int n) {
int lis[] = new int[n];
int lds[] = new int[n];
for(int i = 0; i < n; i++) {
lis[i] = 1;
lds[i] = 1;
}
for(int i = 1; i < n; i++) {
for(int j = 0; j < i; j++) {
if(arr[i] > arr[j] && lis[i] < lis[j]+1) {
lis[i] = lis[j]+1;
}
}
}
for(int i = n-2; i >= 0; i--) {
for(int j = n-1; j > i; j--) {
if(arr[i] > arr[j] && lds[i] < lds[j]+1) {
lds[i] = lds[j]+1;
}
}
}
int max = 0;
for(int i = 0; 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 "+ biotonicSubsequence.lbs(arr, n));
}
}