Can u help me why this one is giving runtime error but giving the correct answer in my pc

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

int cmpfunc (const void * a, const void * b)
{
   return ( *(int*)a - *(int*)b );
}


main()
{
	int N,D;
	scanf("%d %d",&N,&D);
	int *L;
	L = (int*)malloc(N*sizeof(int));
	//int k[N];
	int i;
	for(i = 0;i<N;i++)
	{
		//k[i] = 0;
		scanf("%d",(L+i));	
	}
//	quicksort(L,0,N-1);
qsort(L, N , sizeof(int), cmpfunc);
	
	
	
	int count = 0;
	i = 0;
	while(i<N)
    {
                      if(L[i+1]-L[i]<=D && i<=N-2)
                      {i+=2;count++;}
                      else
                      i++;
    }
					  
	printf("%d",count);

}

Please check your if statement inside while loop. predicate i<=n-2 should come first…

1 Like