Skip to content

Commit 76ba7f0

Browse files
committed
AlgoExpert Three Number Sum
1 parent 01c7e48 commit 76ba7f0

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

README.md

+3
Original file line numberDiff line numberDiff line change
@@ -1091,6 +1091,7 @@ First column links to the problem in AlgoExpert, second is the problem's difficu
10911091
| [Selection Sort][ae&selection-sort] | 🟢 Easy | [![python](res/py.png)][ae&selection-sort#py] |
10921092
| [Sorted Squared Array][ae&sorted-squared-array] | 🟢 Easy | [![python](res/py.png)][ae&sorted-squared-array#py] |
10931093
| [Spiral Traverse][ae&spiral-traverse] | 🟠 Medium | [![python](res/py.png)][ae&spiral-traverse#py] |
1094+
| [Three Number Sum][ae&three-number-sum] | 🟠 Medium | [![python](res/py.png)][ae&three-number-sum#py] |
10941095
| [Two Number Sum][ae&two-number-sum] | 🟢 Easy | [![python](res/py.png)][ae&two-number-sum#py] |
10951096
| [Validate BST][ae&validate-bst] | 🟠 Medium | [![python](res/py.png)][ae&validate-bst#py] |
10961097
| [Validate Subsequence][ae&validate-subsequence] | 🟢 Easy | [![python](res/py.png)][ae&validate-subsequence#py] |
@@ -1146,6 +1147,8 @@ First column links to the problem in AlgoExpert, second is the problem's difficu
11461147
[ae&sorted-squared-array#py]: algoexpert/sorted-squared-array.py
11471148
[ae&spiral-traverse]: https://www.algoexpert.io/questions/spiral-traverse
11481149
[ae&spiral-traverse#py]: algoexpert/spiral-traverse.py
1150+
[ae&three-number-sum]: https://www.algoexpert.io/questions/three-number-sum
1151+
[ae&three-number-sum#py]: algoexpert/three-number-sum.py
11491152
[ae&two-number-sum]: https://www.algoexpert.io/questions/two-number-sum
11501153
[ae&two-number-sum#py]: algoexpert/two-number-sum.py
11511154
[ae&validate-bst]: https://www.algoexpert.io/questions/validate-bst

algoexpert/three-number-sum.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# Three Number Sum
2+
# 🟠 Medium
3+
#
4+
# https://www.algoexpert.io/questions/three-number-sum
5+
#
6+
# Tags: Array, Two Pointer, Sorting
7+
8+
import timeit
9+
10+
11+
# Find all combinations of three numbers in the input that add up to the
12+
# target sum by fixing one number at a time and using two pointers to
13+
# find combinations of other two numbers that make up to the sum.
14+
#
15+
# Time complexity: O(n^2) - For each value in the array, we explore all
16+
# combinations of other two values in linear time.
17+
# Space complexity: O(1) - If we don't count the output or input arrays.
18+
class Solution:
19+
def threeNumberSum(self, array, targetSum):
20+
array.sort()
21+
res = []
22+
# Fix the leftmost element, then use two pointers to find all
23+
# combinations of other two values that add to the target sum.
24+
for i in range(len(array) - 2):
25+
l, r = i + 1, len(array) - 1
26+
while l < r:
27+
val = array[i] + array[l] + array[r]
28+
if val == targetSum:
29+
res.append((array[i], array[l], array[r]))
30+
l += 1
31+
r -= 1
32+
elif val < targetSum:
33+
l += 1
34+
else:
35+
r -= 1
36+
return res
37+
38+
39+
def test():
40+
executors = [
41+
Solution,
42+
]
43+
tests = [
44+
[[8, 10, -2, 49, 14], 57, [(-2, 10, 49)]],
45+
[[1, 2, 3, 4, 5, 6, 7, 8, 9, 15], 29, [(5, 9, 15), (6, 8, 15)]],
46+
[[12, 3, 1, 2, -6, 5, -8, 6], 0, [(-8, 2, 6), (-8, 3, 5), (-6, 1, 5)]],
47+
]
48+
for executor in executors:
49+
start = timeit.default_timer()
50+
for _ in range(1):
51+
for col, t in enumerate(tests):
52+
sol = executor()
53+
result = sol.threeNumberSum(t[0], t[1])
54+
exp = t[2]
55+
assert result == exp, (
56+
f"\033[93m» {result} <> {exp}\033[91m for"
57+
+ f" test {col} using \033[1m{executor.__name__}"
58+
)
59+
stop = timeit.default_timer()
60+
used = str(round(stop - start, 5))
61+
cols = "{0:20}{1:10}{2:10}"
62+
res = cols.format(executor.__name__, used, "seconds")
63+
print(f"\033[92m» {res}\033[0m")
64+
65+
66+
test()

0 commit comments

Comments
 (0)