Closest Numbers

  • + 0 comments

    Here is my solution using Python3:

    def closestNumbers(arr):
        # Write your code here
        result = []
        minDiff = sys.maxsize
        
        # Sort the list in ascending order
        arr.sort()
        
        # Loop to find the minimum difference of two pairs
        for i in range(len(arr)-1):
            if (arr[i+1]-arr[i]) < minDiff:
                minDiff = arr[i+1]-arr[i]
        
        # Loop to add the minimum difference pairs to the result list
        for i in range(len(arr)-1):
            if (arr[i+1]-arr[i]) == minDiff:
                result.append(arr[i])
                result.append(arr[i+1])
        
        return result