Minimum Absolute Difference

Given an array of integers A, find and return the minimum value of ** | A [ i ] - A [ j ] | **
where i != j and | x | denotes the absolute value of x.



Input Format

The only argument given is the integer array A.

Output Format

Return the minimum value of | A[i] - A[j] |.

Constraints

1 <= length of the array <= 100000
-10^9 <= A[i] <= 10^9 

For Example

Input 1:
    A = [1, 2, 3, 4, 5]
Output 1:
    1

Input 2:
    A = [5, 17, 100, 11]
Output 2:
    6
思路:

Sort

Solution:

Time: O(nlogn)
Space: O(1)

public class Solution {
    public int solve(ArrayList<Integer> A) {
        Collections.sort(A);
        int min = Integer.MAX_VALUE;
        for (int i = 1; i < A.size(); i ++) {
            min = Math.min(min, Math.abs(A.get(i) - A.get(i - 1)));
        }
        return min;
    }
}