Minimum Absolute Difference in an Array

  • + 0 comments

    Java and O(n log n)

     Collections.sort(arr);
            int minDiff = Integer.MAX_VALUE;
            for (int i = 1; i < arr.size(); i++) {
                int diff = Math.abs(arr.get(i) - arr.get(i - 1));
                if (diff < minDiff) {
                    minDiff = diff;
                }
            }
            return minDiff;