Minimum Absolute Difference in an Array

  • + 0 comments
    def minimumAbsoluteDifference(arr):
        # Sort the array
        arr.sort()
        
        # Initialize the minimum difference as a large number
        min_diff = float('inf')
        
        # Traverse the sorted array and find the minimum difference
        for i in range(len(arr) - 1):
            diff = abs(arr[i] - arr[i + 1])
            if diff < min_diff:
                min_diff = diff
        
        return min_diff