How to over come the TLE problem in this code

#include
using namespace std;
int main()
{
int t;
cin>>t;
for(int j=0;j<t;j++)
{
int n,ml;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
cin>>a[i];
int inc[n],dec[n];
inc[0]=1;
dec[n-1]=1;
for(int i=1;i<n;i++)
if(a[i]>=a[i-1])
inc[i]=inc[i-1]+1;
else
inc[i]=1;
for(int i=n-2;i>=0;i–)
if(a[i]>=a[i+1])
dec[i]=dec[i+1]+1;
else
dec[i]=1;
ml=inc[0]+dec[0]-1;
for(int i=1;i<n;i++)
if(ml<inc[i]+dec[i]-1)
ml=inc[i]+dec[i]-1;
cout<<ml<<endl;
}
return 0;
}

Your code seems to be O(n), this should not have TLE. Try using “/n” instead of endl, and can also use fast input output, add these line in the begining of main func.
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Please provide link of code so that I can have a better look.

1 Like

thank you for the telling about fast input and output ,it really helped in TLE problem.

but why it is so…

cin and cout are slower than printf and scanf, to achieve the same speed we need to use these lines, first line turns of synchronization of streams, and second line ensure that out put is flushed before using cin.

1 Like

thank you for clearing my doubt it really helped me.

Welcome:hugs::hugs::hugs::hugs::hugs: