Why I got Runtime error (SIGSEGV) in my code for TLG?

I have read some in some forums that this error occurs due to memory, to resolve it we should globally declare the large size arrays and see in the array going out of index or memory. But I have checked all these in my code and all is seeming correct, and it works fine on ideone and on my PC but not on Codechef can anyone please tell me where am I wrong?

Also it is executing correctly on codechef IDE.

#include <algorithm>
#include <stdio.h>
#include <iostream>
using namespace std;
int  s[1000], t[1000];
int main()
{
long int n;
scanf("%ld", &n);
long int i;
for(i=0; i<n; i++)
{
	scanf("\n%d%d", &s[i], &t[i]);
	if(s[i]>t[i])
	{
		s[i]-=t[i];
		t[i]=0;
	}
	else
		{
			t[i]-=s[i];
			s[i]=0;
		}
}
sort(s, s+n);
sort(t, t+n);
//cout<<"\n s["<<n-1<<"]="<<s[n-1]<<"\t t["<<n-1<<"]="<<t[n-1]<<endl;
if(s[n-1]>t[n-1])
	printf("1 %d", s[n-1]);
else
	printf("2 %d", t[n-1]);
}

If this is the problem you are trying to solve, then I’d like to point out that the limit on N is 10000. In your code, the arrays s[] and t[] have a size of 1000. So arrays index is going out of bounds and that’s why you are getting a SIGSEGV.

1 Like