Why is my code here is showing runtime error, I checked it thoroughly. Can anyone suggest any improvement.
Any sorts of debugging or other way to solve this problem is appreciated.
My Solution : - #include
#include<bits/stdc++.h>
#include<stdlib.h>
#include<string.h>
#include<math.h> #include #define wl int t;cin>>t;while(t–) #define l long #define ll long long #define ul unsigned long #define fast std::ios::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
const unsigned int m= 1000000007;
int main()
{
fast
wl
{
int n,k;
cin>>n>>k;
int a[n];
for(int p=0;p<n;p++)
cin>>a[p];
int s = n-k;
int max =0, le=0,j,i;
for( i= 0; i<=s; i++)
{
int c=0;
for( j=i+1;j<=j+k-3;j++)
{
if(a[j]>a[j-1] && a[j]>a[j+1])
c++;
}
if(c>max)
{
max = c;
le = i+1;
}
}
cout<<max+1<<" "<<le<<endl;
}
return 0;
}
There are many errors. You have used wrong condition in second for loop (j<=j+k-3). Even if you correct it, this will give TLE as 3≤k≤n≤2⋅10^5. Your comp is O(k*n).
You can see the solution of other people or wait for editorial.
don’t you think this line doesn’t make any sense instead use for( j=i+1;j<i+k-1;j++). Moreover still it will give tle so use prefix sum and for help you may check my submission Submission #77818057 - Codeforces