This is my Code
include iostream
include algorithm
using namespace std;
//find first increasing than decreasing subarray
int fitdsa(int* arr, int n){
bool started = false;
bool decreasing = false;
int current_counter=0;
int max_count = 0;
for(int i=0; i<n; i++){
if(arr[i]==1){
if(!started){
started=true;
if(max_count<current_counter){
max_count=current_counter;
}
current_counter=0;
current_counter++;
}
if(started&&decreasing){
started=true;
decreasing=false;
if(max_count<current_counter){
max_count=current_counter;
}
current_counter=0;
current_counter++;
}
if(started&&!decreasing){
current_counter++;
}
}
if(arr[i]==-1){
if(decreasing&&started){
current_counter++;
}
if(!decreasing&&started){
current_counter++;
decreasing=true;
}
}
if(arr[i]==0){
if(max_count<current_counter){
max_count=current_counter;
}
current_counter=0;
}
}
if(max_count<current_counter){
max_count=current_counter;
}
return max_count;
}
int main(){
int t;
cin >> t;
while(t–){
int n;
cin>>n;
int* arr = new int[n];
int* idarr = new int[n];
int* diarr = new int[n];
for(int i=0; i<n; i++){
cin >> arr[i];
if(i==0){
idarr[i]=0;
diarr[i]=0;
}
else{
if(arr[i]>arr[i-1]){
idarr[i]=1;
diarr[i]=-1;
}
else if(arr[i]<arr[i-1]){
idarr[i]=-1;
diarr[i]=1;
}
else{
idarr[i]=0;
diarr[i]=0;
}
}
}
int idmaxe = fitdsa(idarr, n); //Increasing Decreasing Max Element
int dimaxe = fitdsa(diarr, n);
int maxe = max(dimaxe, idmaxe);
cout << maxe << endl;
delete []diarr;
delete []idarr;
delete []arr;
}
return 0;
}