Only testcase 0 is working

#include<bits/stdc++.h>
using namespace std;
int main() {
int t; cin >> t;
while(t > 0){
int n; cin >> n;
int arr[n];
for(int i = 0; i < n; i++)
cin >> arr[i];
int max_length = 1;
int curr_length = 1;
int i =1;
while(i < n){
if(arr[i] == arr[i-1]){
curr_length++;
if(max_length < curr_length)
curr_length = max_length;
}
else if(arr[i] > arr[i-1]){
curr_length++;
if(max_length <curr_length)
max_length = curr_length;
}
else if(arr[i] < arr[i-1]){
curr_length++;
if(max_length < curr_length)
max_length = curr_length;
if(i != n-1 && arr[i+1] > arr[i])
curr_length = 1;
}
i++;

	}
	cout<<max_length<<endl;
	t--;

}
return 0;

}

Please tell me the flaw in my logic. I’ve tried it on a lot of test cases, still, only test case 0 is working. Any hint or resolution?

hello @siddhu28.ss
the idea is that for each index i .
we will find what is the maximum length of increasing sequence that is ending at index i (let say it is t1).
and then we will find what is the maximum length of decreasing sequence that is starting from index i (let say it is t2).
now our answer will be t1+t2-1 ( -1 becuae ith element is considered twice).

now repeat this process for each value of i . and print maximum value of t1+t2-1.

here is the optimised approach based on the same idea.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.