From ca62f9158c376efbe1f176d9d2e77f4d1c2a106c Mon Sep 17 00:00:00 2001 From: Rohan Karan Date: Tue, 28 Dec 2021 18:45:08 +0530 Subject: [PATCH] chore(Python): add two sum for Dictionaries (#656) --- algorithms/Python/README.md | 3 ++ algorithms/Python/dictionaries/two-sum.py | 44 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 algorithms/Python/dictionaries/two-sum.py diff --git a/algorithms/Python/README.md b/algorithms/Python/README.md index 2500e211..c2575be5 100644 --- a/algorithms/Python/README.md +++ b/algorithms/Python/README.md @@ -11,6 +11,9 @@ - [Singly](linked_lists/singly.py) - [Reverse List](linked_lists/reverse-linkedlist.py) +## Dictionaries +- [Two Sum](dictionaries/two-sum.py) + ## Multiplication - [Karatsuba](multiplication/karatsuba.py) diff --git a/algorithms/Python/dictionaries/two-sum.py b/algorithms/Python/dictionaries/two-sum.py new file mode 100644 index 00000000..de46bffe --- /dev/null +++ b/algorithms/Python/dictionaries/two-sum.py @@ -0,0 +1,44 @@ +""" +Problem: +Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to +target. You can not use the same element twice. +You can return any one of the answers in any order, if not found return [-1, -1]. + + +Solution: +In this solution we check if each element's complement i.e. difference between the target and the element +(target−nums[i]) exists in the dictionary. If it does exist, we return current element's index and its complement's +index else we store the complement in the dictionary. +Beware that the complement must not be nums[i] itself, because we can not use the same element twice. + +Complexity Analysis: +Time complexity: O(n). We traverse the list containing nn elements only once. Each lookup in the dictionary costs only +O(1) time. +Space complexity: O(n). The extra space required depends on the number of items stored in the dictionary, +which stores at most nn elements. +""" + + +def twoSum(nums, target): + dictionary = {} + # using enumerate to iterate through the list keeping track of both index and element + for i, val in enumerate(nums): + diff = target - val + if diff in dictionary: + return [dictionary[diff], i] + dictionary[val] = i + return [-1, -1] + + +def main(): + try: + arr = list(map(int, input("Enter space-seperated integers: ").split())) + tar = int(input("Enter target: ")) + except ValueError: + arr = [2, 7, 11, 15] + tar = 9 + print(twoSum(arr, tar)) + + +if __name__ == "__main__": + main()