I am trying to solve Longest Bi tonic sub-sequence problem, although I got the running code from various sites, but I cannot figure out why my solution is not working. I am trying to return the max length sub-sequence. I have created a type named array which stores the value for every element, if it is included in ascending sub-sequence or descending sub-sequence.
#include <bits/stdc++.h>
#define li long int
int longestBitonicSubarray(int *input, int n) {
li* output = new li[n];
li* type = new li[n];
output[0] = 1;
type[0] = 0;
/*
type when 1 means order is ascending
type when 2 means order is descending
type when 0 not decided
*/
for(li i=1;i<n;i++) {
output[i] = 1;
type[i] = 0;
for(li j=i-1;j>=0;j--) {
if(type[j] == 0 || type[j] == 1) {
if(input[j] < input[i]) {
li pos_ans = output[j] + 1;
if(output[i] < pos_ans) {
output[i] = pos_ans;
type[i] = 1;
}
} else if(input[j] > input[i]) {
li pos_ans = output[j] + 1;
if(output[i] < pos_ans) {
output[i] = pos_ans;
type[i] = 2;
}
}
} else {
if(input[j] > input[i]) {
li pos_ans = output[j] + 1;
if(output[i] < pos_ans) {
output[i] = pos_ans;
type[i] = 2;
}
}
}
}
}
li ans = *max_element(output,output+n);
delete [] output;
delete [] type;
return ans;
}