Skip to content

Commit 965e027

Browse files
Added python solution for 2sum problem
1 parent 62efcb4 commit 965e027

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

twoSumusingHashMap.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
2+
'''The below approach is using Hash data structure where we store only the unique values of the array
3+
This approach can be extended to 3sum problem as well
4+
'''
5+
def printPairs(arr, arr_size, sum):
6+
hashmap = {}
7+
8+
for i in range(0, arr_size):
9+
temp = sum-arr[i]
10+
if (temp in hashmap):
11+
print('Yes')
12+
return
13+
hashmap[arr[i]] = i
14+
print("No")
15+
16+
17+
A = [1, 4, 45, 6, 10, 8]
18+
n = 16
19+
printPairs(A, len(A), n)

0 commit comments

Comments
 (0)