Alway's keep getting a Runtime Error(SIGSEGV)

#include<stdio.h>
int main()
{
long a[100],sum=0;
int ans=0,n=0,d=0,i=0, ratio=0, t=0, dif=0;
scanf("%d", &t);
while(t–>0){
scanf("%d %d",&n, &d);
for(i=0;i<n;i++)
{
scanf("%ld", &a[i]);
sum+=a[i];
}

	if(sum%n==0)
	{
		ratio=sum/n;
	
	for(i=0;i<n-d;i++)
	{
			if(a[i]>ratio)
			{
				dif=a[i]-ratio;
				a[i]-=dif;
				ans+=dif;
				a[i+d]+=dif;

			}
			else if(a[i]<ratio)
			{
				dif=ratio-a[i];
				a[i]+=dif;
				ans+=dif;
				a[i+d]-=dif;
			}
	}
		for(i=0;i<n;i++)
	{
		if(a[i]!=ratio)
			ans=-1;
	}
	}
	else
		ans=-1;
	printf("%d",ans);
}
return 0;

}

There is nothing wrong with the code and I have initialized all the variables too.

can you tell which problem you are trying this solution.

https://www.codechef.com/problems/CHEFMOVR - This one

Hey,

I’m not sure, if you are getting a run time error in your code that’s generally because you are accessing the index which is not present. let’s suppose n > 100 than your solution will throw a runtime error. Since many programming questions has n as great as 10^5. If that’s not the case… please comment i’ll try to help.

// long a[100];

Hope this helps!

checked, now you program is not giving any run time error.

Hello, thanks for helping me out.
Can you tell me what exactly you changed in the program? Did you increase the length of the array?

Yeah, instead of using long a[100] use long a[100005] because the N can go up to 10^5. Though it’s giving WA. I want you to figure it out yourself(Why WA). This will help you to debug your code in future.