 # Chef Feeds Cats run time error (other)?

i don’t know what is causing this error as the constraints range is not too large . can anyone tell me what is causing that error?

``````#include<algorithm>
using namespace std;
int main()
{
int T;
//cout<<"enter number of test cases ";
cin>>T;
while(T--)
{
int N,M;
//cout<<"enter value of N";
cin>>N>>M;

int A[M],i,j;

for(i=0;i<M;++i)
{
cin>>A[i];

}

int c=0;
for(i=0;i<M;i+=N)
{
sort(A+i,A+(i+N));

for(j=i+1;j<N+i;++j)
{
if(A[j-1]==A[j])
{
c++;
}
}
}

if(c==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;

}
return 0;
}`````````

Since N > M is possible, let’s consider the test case:

``````100 1
1
``````

`line 26`
In the first iteration of the `for` loop at `line 24`, the second operand of function `sort` tries to access a memory location, that really, isn’t legal to access. So, that’s producing a Segmentation Fault. 2 Likes
``````        {
for(i=0;i<M;i+=N)
{
sort(A+i,A+(i+N));

for(j=i+1;j<N+i;++j)
{
if(A[j-1]==A[j])
{
c++;
}
}
}
}
``````

is this gonna solve the issue as now it will enter only when M>1

I’m sorry I don’t quite think that that’s going to work either. When your code runs against my test case, even for `M = 2`, it’s going to try to access location `&A + 0x00000063` in the first iteration itself and that’s going to cause a Seg. Fault because `A` is only 8 bytes long. You may take a look at this documented piece of C++14 code. I tried explaining every part. I hope that helps. 2 Likes