Skip to content

Commit 8113b9a

Browse files
added two_num.py, [ques]Sum of 2 numbers being a given target.
1 parent 0099aca commit 8113b9a

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

two_num.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"""Author Anurag Kumar (mailto:anuragkumarak95@gmail.com)
2+
3+
Given an array of integers, return indices of the two numbers such that they add up to a specific target.
4+
You may assume that each input would have exactly one solution, and you may not use the same element twice.
5+
6+
Example:
7+
Given nums = [2, 7, 11, 15], target = 9,
8+
Because nums[0] + nums[1] = 2 + 7 = 9,
9+
return [0, 1].
10+
11+
"""
12+
13+
def twoSum(nums, target):
14+
chk_map = {}
15+
for index, val in enumerate(nums):
16+
compl = target - val
17+
if compl in chk_map:
18+
indices = [chk_map[compl], index]
19+
print(indices)
20+
return [indices]
21+
else:
22+
chk_map[val] = index
23+
return False

0 commit comments

Comments
 (0)