-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path001. 2 number sum.py
65 lines (51 loc) · 1.94 KB
/
001. 2 number sum.py
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# 2 number sum
"""You are given an array of the distinct int & a target sum.
We need to write a function to find 2 number which lead to target sum"""
# Naive Solution
"""Just using a nested loop and going to every element and checking the sum"""
# O(n^2) Time complexity / O(1) Space complexity
def twoNumberSumNaive(array, targetSum):
for i in range(len(array)):
firstNum = array[i]
for j in range(i + 1, len(array)):
secondNum = array[j]
if firstNum + secondNum == targetSum:
return [firstNum, secondNum]
return []
# Effective solution
"""We make a hash table and then store sum that will lead to target sum then
search for matching values in the array, we will get our result.
Since, we are traversing array twice but not in a nested way, so we get
less complexity"""
# O(n) Time Complexity / O(n) Space Complexity
def twoNumberSumEff(array, targetSum):
nums = {}
for num in array:
if targetSum - num in nums:
return [targetSum-num, num]
else:
nums[num] = True
return []
# Effective solution
"""A very basic approach for array solving is sorting it, So
we sort the array and then make 2 pointer(left and right)
So we just perform a sum and get the desired output."""
# O(n log(n)) Time Complexity / O(1) Space Complexity
# O(n) Time Complexity / O(n) Space Complexity
def twoNumberSumSort(array, targetSum):
array.sort()
left = 0
right = len(array) - 1
while left < right:
print(left, right)
currentSum = array[left] + array[right]
if currentSum == targetSum:
return [array[left], array[right]]
elif currentSum < targetSum:
left += 1
elif currentSum > targetSum:
right -= 1
return []
print(twoNumberSumNaive([3, 5, -4, 8, 11, 1, -1, 6], 10))
print(twoNumberSumEff([1, 2, 3], 5))
print(twoNumberSumSort([12, 3, 1, 2, -6, 5, -8, 6], 0))