Absolute Difference Between Any Two elements of An Array



import java.util.Arrays;


public class diffBWtwoElements {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        int a[] = { 5, -3, 7, -2 };

        Arrays.sort(a);
        int diff = Integer.MAX_VALUE;
        for (int i = 0; ia.length - 1; i++) {
            if (a[i + 1] - a[i] < diff) {
                diff = a[i + 1] - a[i];
            }
        }
        System.out.println(diff);

    }



}

Comments