-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlongestBitonicSubsequence.cpp
More file actions
50 lines (39 loc) · 1020 Bytes
/
longestBitonicSubsequence.cpp
File metadata and controls
50 lines (39 loc) · 1020 Bytes
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
int longestBitonicSubarray(int *input, int n) {
/* Don't write main().
* the input is already passed as function argument.
* Taking input and printing output is handled automatically.
*/
int *increasing = new int[n];
int *decreasing = new int[n];
for(int i = 0; i < n; i++)
{
increasing[i] = 1;
for(int j = 0; j < i; j++)
{
if(input[i] > input[j])
{
increasing[i] = max(increasing[i], 1+increasing[j]);
}
}
}
for(int i = n-1; i > 0; i--)
{
decreasing[i] = 1;
for(int j = n-1; j > i; j--)
{
if(input[i] > input[j])
{
decreasing[i] = max(decreasing[i], 1+decreasing[j]);
}
}
}
int maxLength = 0;
for(int i = 0; i < n; i++)
{
// cout << i << " : " << increasing[i] << " " << decreasing[i] << endl;
maxLength = max(maxLength, increasing[i] + decreasing[i]);
}
delete [] increasing;
delete [] decreasing;
return maxLength-1;
}