Skip to content

Commit bd822ad

Browse files
committed
twoSum problem
1 parent 517be7a commit bd822ad

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

twoSum.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# two sum is a problem where we need to find the pair which sum up to some particular value let say 7
2+
# here we use hash table for solving these problem which make its complexity O(n)
3+
4+
def twoSum(arr, target):
5+
store = {} # dictionary hash table
6+
for i in range(len(arr)):
7+
if arr[i] in store:
8+
return [store[arr[i]], i]
9+
else:
10+
store[target - arr[i]] = i
11+
12+
13+
arr = [1, 2, 6, 3, 9]
14+
print(twoSum(arr, 7))

0 commit comments

Comments
 (0)