Problem 1: Two Numbers Target Sum: Array Problem - O(n) Time and Space Complexity

Problem Statement:

Write a function that takes in a non-empty array of distinct integers and an integer representing a target sum. If any two numbers in the input array sum up to the target sum, the function should return them in an array, in any order. If no two numbers sum up to the target sum, the function should return an empty array.

Note: the target sum has to be obtained by summing two different integers in the array; you can’t add a single integer to itself in order to obtain the target sum.

You can assume that there will be at most one pair of numbers summing up to the target sum. It should return empty list if no pair found.

Problem 2 Link:
https://discuss.boardinfinity.com/t/problem-2-sorted-square-array-array-problem-o-n-optimal-time-complexity/12456

Solution: Using Hash Table

def twoNumberSum(array, targetSum):
    #Anish Arya O(n) Time and Space Complexity
    matchHashTable = {}
    for val in array:
        toBeMatched = targetSum - val
        if toBeMatched in matchHashTable:
            return [toBeMatched, val]
        matchHashTable[val] = 1
    return []

Solution: Using N*N
Assuming that there will be at most one pair of numbers summing up to the target sum. It should return empty list if no pair found.

def twoNumberSum(array, targetSum):
    # Write your code here. // Anish Arya O(n^2)
	for idx, val in enumerate(array):
		try:
			for val2 in array[idx + 1:]:
				if val + val2 == targetSum:
					return [val, val2]
		except:
			return []
    return []