-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathTwo_Sum.py
More file actions
31 lines (26 loc) · 731 Bytes
/
Two_Sum.py
File metadata and controls
31 lines (26 loc) · 731 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"""
@author - Anirudh Sharma
"""
from typing import List
def twoSum(nums: List[int], target: int) -> List[int]:
# List to store results
result = []
# Dictionary to store the difference and its index
index_map = {}
# Loop for each element
for i, n in enumerate(nums):
# Difference which needs to be checked
difference = target - n
if difference in index_map:
result.append(i)
result.append(index_map[difference])
break
else:
index_map[n] = i
return result
numbers = [2, 7, 11, 15]
target_value = 9
print(str(twoSum(numbers, target_value)))
numbers = [3, 2, 4]
target_value = 6
print(str(twoSum(numbers, target_value)))