Minimum Absolute Difference in an Array

  • + 1 comment

    with your approach i am able to pass only 2 test cases, but with my code i am unable to pass only 4th test case,Suggest some changes or another approach. Here is my code in C:

    #include<stdio.h>
    #include<stdlib.h>
    main()
    {
        long int i,j,n;
        scanf("%ld",&n);
        long long int min,a[n];
        scanf("%lld%lld",&a[0],&a[1]);
        min=abs(a[0]-a[1]);
        for(i=2;i<n;i++)
        {
            scanf("%lld",&a[i]);
            for(j=0;j<i;j++)
            {
                if(abs(a[j]-a[i])<min)
                    min=abs(a[j]-a[i]);
                if(min==0)
                {
                    break;
                }
            }
            if(min==0)
                break;
        }
        printf("%lld",min);
    }