Minimum Absolute Difference in an Array

  • + 0 comments

    My Java solution with o(n log n + n) time complexity and o(1) space:

    public static int minimumAbsoluteDifference(List<Integer> arr) {
            // goal: determine the min abs val between any two pairs in arr
            Collections.sort(arr); //sort arr ascending
            int min = Integer.MAX_VALUE;
            for(int i = 1; i < arr.size(); i++){
                int currentMin = arr.get(i) - arr.get(i - 1); 
                if(currentMin < min) min = currentMin;
            }
            return min;
        }