def closest_nums(N, first, second): closest_pair = None lookup = {N - x for x in first} sorted_lookup = sorted(list(lookup)) for y in second: # dichotomy x = find_closest(y, sorted_lookup) if abs(x - y) < abs(closest_pair.x - closest_pair.y): closest_pair = Pair(x, y) return closest_pair