-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3sum.py
More file actions
30 lines (26 loc) · 751 Bytes
/
3sum.py
File metadata and controls
30 lines (26 loc) · 751 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
27
28
29
30
def find2sum(array: list, value: int) -> list:
i, j = 0, len(array) - 1
pairs = []
while i < j:
current_sum = array[i] + array[j]
if current_sum > value:
j -= 1
if current_sum < value:
i += 1
if current_sum == value:
pairs.append((array[i], array[j]))
i += 1
j -= 1
return pairs
def find3sum(array: list) -> list:
size = len(array)
data = sorted(array)
triples = set()
for i in range(size-1):
pairs = find2sum(data[i+1:], -data[i])
for a, b in pairs:
triples.add((data[i], a, b))
return list(triples)
if __name__ == "__main__":
result = find3sum([-1, 0, 1, 2, -1, -4])
print(result)