# Write a function that takes in two non-empty arrays of integers, finds the pair 
# of numbers (one from each array) whose absolute difference is closest to zero, 
# and returns an array containing these two numbers, with the number from the 
# first array in the first position.

# Note that the absolute difference of two integers is the distance between them 
# on the real number line. For example, the absolute difference of -5 and 5 is 
# 10, and the absolute difference of -5 and -4 is 1.

# You can assume that there will only be one pair of numbers with the smallest 
# difference.

# Sample input:
# arr1 = [3, 5, 28, -1, 10, 20]
# arr2 = [135, 134, 17, 15, 26]
# Sample output:
# [28, 26]

def smallestDifference(arr1, arr2):
    arr1.sort()
    arr2.sort()
    smallest_pair = None
    smallest_diff = float('inf')
    i, j = 0, len(arr2) - 1

    while i < len(arr1) and j >= 0:
        diff = abs(arr1[i] - arr2[j])
        pair = (arr1[i], arr2[j])
        if diff == 0:
            return pair

        if diff < smallest_diff:
            smallest_pair = pair
            smallest_diff = diff

        if arr1[i] > arr2[j]:
            i += 1
        else:
            j -= 1

    return smallest_pair

arr1 = [3, 5, 28, -1, 10, 20]
arr2 = [135, 134, 17, 15, 26]
print(smallestDifference(arr1, arr2))

Embed on website

To embed this program on your website, copy the following code and paste it into your website's HTML: