How can I write an algorithm to check if the sum of any two numbers in an array/list matches a given number with a complexity of nlogn?
nlogn
def sum_in(numbers, sum_): """whether any two numbers from `numbers` form `sum_`.""" a = set(numbers) # O(n) return any((sum_ - n) in a for n in a) # O(n)
Example:
>>> sum_in([200, -10, -100], 100) True