Closest Numbers

  • + 0 comments
    def closestNumbers(arr):
        # Write your code here
        arr.sort()
        min_abs = abs(arr[0] - arr[1])
        min_arr = [arr[0], arr[1]]
        for i in range(1, len(arr)-1):
            if abs(arr[i] - arr[i+1]) > min_abs:
                continue
            elif abs(arr[i] - arr[i+1]) == min_abs:
                min_arr.append(arr[i])
                min_arr.append(arr[i+1])
            elif abs(arr[i] - arr[i+1]) < min_abs:
                min_abs = abs(arr[i] - arr[i+1])
                min_arr = [arr[i], arr[i+1]]
        return min_arr