DEV Community

Discussion on: Daily Challenge #63- Two Sum

colorfusion profile image
Melvin Yeo • Edited on

In Python, written with a O(n) solution with the given assumptions using a dictionary and enumeration to generate an index.

def twoSum(arr: list, target: int) -> (int, int):
    num_dict = dict()
    for index, num in enumerate(arr):
        diff = target - num
        if diff in num_dict:
            return (min(index, num_dict[diff]), max(index, num_dict[diff]))
        num_dict[num] = index

    raise IndexError