-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathThreeNumbers.py
More file actions
26 lines (22 loc) · 803 Bytes
/
ThreeNumbers.py
File metadata and controls
26 lines (22 loc) · 803 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
# Three number sum program
# We will be using the sliding window algorithm here as well which will give us the complexity of O(nlogn)
def sum_of_three(arr, target) :
result = list()
arr.sort()
for i in range(len(arr)-2) :
left = i+1;right=len(arr)-1
while left < right :
current_sum = arr[i] + arr[left] + arr[right]
if current_sum == target :
result.append((arr[i], arr[left], arr[right]))
left+=1
right-=1
elif current_sum < target :
left += 1
elif current_sum > target :
right -= 1
else :
print("Dont with the loop")
return result
if __name__ == '__main__' :
print(sum_of_three([3,5,-4,8,11,1,-1,6], 10))