divquery problem

hallo, I just registrated and saw this interesting problem here http://www.codechef.com/problems/DIVQUERY/ ,I make some code I and I think it is fine ,when I type the same input like in the problem the output is the same ,but It is giving me runtime error.Here is the code in C :

#include <stdio.h>
#include <stdlib.h>

int main()
{
int N;//sequence of integers
int Q;//how many quiries
int L,R,K;//L-left,R-right,K-number to be divisible
printf(“Enter the number of numbers and the number of quiries\n”);
scanf("%d",&N);
scanf("%d",&Q);
int i;int count=0;int check=0;
int numbers[N];int otgovori[100];int j;
printf(“Now enter the numbers\n”);
for(i=0;i<N;i++)
{
scanf("%i",&numbers[i]);
}
printf(“Now enter L ,R and K\n”);
for(i=0;i<Q;i++){
scanf("%d",&L);
scanf("%d",&R);
scanf("%d",&K);
for(j=L-1;j<R;j++)
{
if(numbers[j]%K==0)
{
check++;
}
}
otgovori[count]=check;
count++;
check=0;
}
for(j=0;j<Q;j++)
{
printf("%i\n",otgovori[j]);
}

}

Hello @dgan89. There main problem in your code is that you have declared your array otgovori,where you are storing answer for each query, of size 100 . However the number of queries can be around 10^5 which is more than the capacity of the array. Change the size of the array to 10^5 and you will not get runtime error.

Also you should declare large arrays globally as there is limited memory in the local heap.You might want to refer to this FAQ for future reference.

After doing this you will get TLE (Time limit exceeded) as your code is higly inefficient. Have a look at this editorial to get an idea on how to solve this problem. It is a difficult problem so you might want to try a few simpler ones from the easy section first and then come back to this later.