Skip to content

Commit 7b9b37f

Browse files
committed
feat(algorithms): find sum of three
1 parent 5986d54 commit 7b9b37f

File tree

4 files changed

+101
-3
lines changed

4 files changed

+101
-3
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Find Sum of Three
2+
3+
Given an array of integers, nums, and an integer value, target, determine if there are any three integers in nums whose
4+
sum is equal to the target, that is, nums[i] + nums[j] + nums[k] == target. Return TRUE if three such integers exist in
5+
the array. Otherwise, return FALSE.
6+
7+
> Note: A valid triplet consists of elements with distinct indexes. This means, for the triplet nums[i], nums[j], and
8+
> nums[k], i ≠ j, i ≠ k and j ≠ k.
9+
10+
Constraints:
11+
- 3 ≤ nums.length ≤ 500
12+
- -10^3 ≤ nums[i] ≤ 10^3
13+
- −10^3 ≤ target ≤ 10^3
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from typing import List
2+
from copy import copy
3+
4+
5+
def find_sum_of_three(nums: List[int], target: int) -> bool:
6+
"""
7+
Checks if there are any three integers in nums whose sum is equal to the target
8+
Args:
9+
nums (list): list of integers
10+
target (int): target to equate to
11+
Returns:
12+
bool: True if there are three integers that sum up to target, False otherwise
13+
"""
14+
# note that using a copy of the original array leads to unnecessary memory consumption
15+
numbers = copy(nums)
16+
# Sorts in place which incurs a Time complexity of O(nlog(n))
17+
numbers.sort()
18+
19+
for idx, num in enumerate(numbers):
20+
if idx > 0 and num == numbers[idx-1]:
21+
continue
22+
23+
left, right = idx+1, len(numbers) - 1
24+
25+
while left < right:
26+
current_sum = num + numbers[left] + numbers[right]
27+
28+
if current_sum > target:
29+
right -= 1
30+
elif current_sum < target:
31+
left += 1
32+
else:
33+
return True
34+
35+
return False
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import unittest
2+
from . import find_sum_of_three
3+
4+
5+
class FindSumOfThreeTestCases(unittest.TestCase):
6+
def test_1(self):
7+
"""should return False for nums=[1,-1,0], target=-1"""
8+
nums = [1, -1, 0]
9+
target = -1
10+
actual = find_sum_of_three(nums, target)
11+
self.assertFalse(actual)
12+
13+
def test2(self):
14+
"""should return True for nums=[3,7,1,2,8,4,5], target=10"""
15+
nums = [3,7,1,2,8,4,5]
16+
target = 10
17+
actual = find_sum_of_three(nums, target)
18+
self.assertTrue(actual)
19+
20+
def test_3(self):
21+
"""should return False for nums=[3,7,1,2,8,4,5], target=21"""
22+
nums = [3,7,1,2,8,4,5]
23+
target = 21
24+
actual = find_sum_of_three(nums, target)
25+
self.assertFalse(actual)
26+
27+
def test_4(self):
28+
"""should return True for nums=[-1,2,1,-4,5,-3], target=-8"""
29+
nums = [-1,2,1,-4,5,-3]
30+
target = -8
31+
actual = find_sum_of_three(nums, target)
32+
self.assertTrue(actual)
33+
34+
def test_5(self):
35+
"""should return True for nums=[-1,2,1,-4,5,-3], target=0"""
36+
nums = [-1,2,1,-4,5,-3]
37+
target = 0
38+
actual = find_sum_of_three(nums, target)
39+
self.assertTrue(actual)
40+
41+
42+
if __name__ == '__main__':
43+
unittest.main()

algorithms/two_pointers/three_sum/README.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,30 @@ and nums[i] + nums[j] + nums[k] == 0.
55

66
Notice that the solution set must not contain duplicate triplets.
77

8+
```plain
89
Example 1:
9-
1010
Input: nums = [-1,0,1,2,-1,-4]
1111
Output: [[-1,-1,2],[-1,0,1]]
1212
Explanation:
1313
nums[0] + nums[1] + nums[2] = (-1) + 0 + 1 = 0.
1414
nums[1] + nums[2] + nums[4] = 0 + 1 + (-1) = 0.
1515
nums[0] + nums[3] + nums[4] = (-1) + 2 + (-1) = 0.
1616
The distinct triplets are [-1,0,1] and [-1,-1,2].
17-
Notice that the order of the output and the order of the triplets does not matter.
17+
```
18+
> Notice that the order of the output and the order of the triplets does not matter.
19+
20+
```plain
1821
Example 2:
1922
2023
Input: nums = [0,1,1]
2124
Output: []
2225
Explanation: The only possible triplet does not sum up to 0.
26+
```
27+
28+
```plain
2329
Example 3:
2430
2531
Input: nums = [0,0,0]
2632
Output: [[0,0,0]]
27-
Explanation: The only possible triplet sums up to 0.
33+
Explanation: The only possible triplet sums up to 0.
34+
```

0 commit comments

Comments
 (0)