Please discuss the algorithm

my code will not work for a[i+1]==a[i]

#include
#include
using namespace std;
int main()
{
int t,n,inc_index,dec_index;
cin>>t;
while(t–)
{
int c1=1,c2=1;
int max_inc=0,max_dec=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
for(int i=0;i<n-1;i++)
{
if(a[i+1]>a[i])
{
c1++;
c2=1;
if(c1>max_inc){
max_inc=c1;
inc_index=i+1;
}
}
else if(a[i+1]<a[i])
{
c2++;
c1=1;
if(c2>max_dec){
max_dec=c2;
dec_index=i+1;
}
}
}
if(inc_index==dec_index-c2+1)
cout<<max_inc+max_dec-1<<endl;
else
{
int mx=max(max_inc,max_dec);
cout<<mx<<endl;
}
}
}

@namangarg31 the way you are measuring the c1 and c2 you will end up with wrong and disconnected array sizes.

@namangarg31
Create two array inc, dec, they will keep count of the length of increasing from till ith pos and decreasing from ith position, now the bitonic length is simply the sum of both arrays.


If this resolves your doubt mark it as resolved

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.