-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitonicSequence.cpp
More file actions
62 lines (54 loc) · 1.07 KB
/
Copy pathBitonicSequence.cpp
File metadata and controls
62 lines (54 loc) · 1.07 KB
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
52
53
54
55
56
57
58
59
60
61
62
/*
You are given array, find the Longest Bitonic Subsequence
Bitonic Sequence is a Sequence which will
Increase and Decrease : /\
Increase : /
Decrease : \
I/P : 10 22 9 33 21 50 41 60 80 3
O/P : 7
TC : O(n * n)
SC : O(n * n)
*/
#include<bits/stdc++.h>
using namespace std;
int Bitonic_Sequence(int arr[], int n){
int lis[n]; //Longest Increasing Subsequence
int lds[n]; //Longest Decreasing Subsequence
memset(lis,0,sizeof lis);
memset(lds,0,sizeof lds);
for(int i=0;i<n;i++){
int max=0;
for(int j=0;j<i;j++){
if(arr[j]<=arr[i]){
if(lis[j]>max){
max=lis[j];
}
}
}
lis[i]=max+1;
}
for(int i=n-1;i>=0;i--){
int max=0;
for(int j=n-1;j>i;j--){
if(arr[j]<=arr[i]){
if(lds[j]>max){
max=lds[j];
}
}
}
lds[i]=max+1;
}
int maxval=0;
for(int i=0;i<n;i++){
if(lis[i]+lds[i]-1>maxval){
maxval=lis[i]+lds[i]-1;
}
}
return maxval;
}
int main(){
int arr[] = {10,22,9,33,21,50,41,60,80,3};
int n = 10;
cout << Bitonic_Sequence(arr, n) << "\n" ;
return 0;
}