Why this code gives 0 output everytime?

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

int main(void) {

int n,d,i,j,k,cnt=0;

scanf("%d %d",&n,&d);

 int *arr=(int*)malloc(n*sizeof(int));


for (i=0;i<(n);i++)
{
    for (j=i+1;j<n;j++)
    {
        if ((arr[j] - arr[i])==d)
        {
            for (k=j+1;k<n;k++)
                if ((arr[k]-arr[j]) ==d) cnt++;
        }
    }

}

printf("%d",cnt);



	return 0;
}

You don’t appear to be initialising the contents of arr anywhere, so it’s unlikely that a pair of elements of arr would differ by exactly d.

Edit:

If d were 0, though, then you might get a non-zero answer - see e.g. here.

1 Like