<jupyter_start><jupyter_text>Smallest Difference[link](https://www.algoexpert.io/questions/Smallest%20Difference)  My Solution<jupyter_code>def smallestDifference(arrayOne, arrayTwo):
    # Write your code here.
    arrayOne.sort() # in place sort, quick sort or merge sort, O(nlog(n))
    arrayTwo.sort()
    oneIdx = 0
    twoIdx = 0
    minPair = [arrayOne[0], arrayTwo[0]]
    minValue = abs(arrayOne[0] - arrayTwo[0])
    while oneIdx < len(arrayOne) and twoIdx < len(arrayTwo):
        diff = arrayOne[oneIdx] - arrayTwo[twoIdx]
        absDiff = abs(diff)
        
        if absDiff < minValue:
            minValue = absDiff
            minPair = [arrayOne[oneIdx], arrayTwo[twoIdx]]
            
        if absDiff == 0:
            break
        elif diff < 0:
            oneIdx += 1
        else:
            twoIdx += 1
    return minPair
arrayOne = [-1, 5, 10, 20, 28, 3]
arrayTwo = [26, 134, 135, 15, 17]
smallestDifference(arrayOne, arrayTwo)<jupyter_output><empty_output><jupyter_text>Expert Solution<jupyter_code># O(nlog(n) + mlog(m)) time | O(1) space
# where n (m) is the length of array one (two)
def smallestDifference(arrayOne, arrayTwo):
    arrayOne.sort()
    arrayTwo.sort()
    idxOne = 0
    idxTwo = 0
    smallest = float("inf")
    current = float("inf")
    smallestPair = []
    while idxOne < len(arrayOne) and idxTwo < len(arrayTwo):
        firstNum = arrayOne[idxOne]
        secondNum = arrayTwo[idxTwo]
        if firstNum < secondNum:
            current = secondNum - firstNum
            idxOne += 1
        elif secondNum < firstNum:
            current = firstNum - secondNum
            idxTwo += 1
        else:
            return [firstNum, secondNum]
        if smallest > current:
            smallest = current
            smallestPair = [firstNum, secondNum]
    return smallestPair<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10718113612004287, 'fraction_numerical': 0.020364415862808145, 'mean_word_length': 2.4256880733944954, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 10, 'https://': 1, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '529794', 'license': 'MIT', 'n_tokens_mistral': 621, 'n_tokens_neox': 592, 'n_words': 149, 'path': 'algoExpert/smallest_difference/solution.ipynb', 'repo_name': 'maple1eaf/learning_algorithm', 'size': '1866'}