Minimum Absolute Difference in an Array

  • + 0 comments
        public static int minimumAbsoluteDifference(List<Integer> arr) {
            // Sort the array
            Collections.sort(arr); 
            
            int absMin = Integer.MAX_VALUE;
            for (int i = 1; i < arr.size(); i++) {
                int absDiff = Math.abs(arr.get(i) - arr.get(i - 1));
                absMin = Math.min(absMin, absDiff);
            }
            
            return absMin;
        }