Skip to content

Commit 1047184

Browse files
committed
feat(algorithms, arrays, binary search): two sum less than k
1 parent f74b0b9 commit 1047184

File tree

8 files changed

+129
-0
lines changed

8 files changed

+129
-0
lines changed

algorithms/arrays/two_sum/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ def two_sum(numbers: List[int], target: int) -> List[int]:
2727
if complement in m:
2828
return [m[complement], idx]
2929
m[num] = idx
30+
return []
3031

3132

3233
def two_sum_with_pointers(numbers: List[int], target: int) -> List[int]:
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Two Sum Less Than K
2+
3+
Given an array of integers, nums, and an integer k, find the maximum sum of two elements in nums less than k. Otherwise,
4+
return −1 if no such pair exists.
5+
6+
Constraints
7+
8+
- 1 ≤ nums.length ≤ 100
9+
- 1 ≤ nums[i] ≤ 10^3
10+
- 1 ≤ k ≤ 10^3
11+
12+
## Examples
13+
14+
![Example 1](./images/examples/two_sum_less_k_example_1.png)
15+
![Example 2](./images/examples/two_sum_less_k_example_2.png)
16+
![Example 3](./images/examples/two_sum_less_k_example_3.png)
17+
![Example 4](./images/examples/two_sum_less_k_example_4.png)
18+
![Example 5](./images/examples/two_sum_less_k_example_5.png)
19+
20+
## Related Topics
21+
22+
- Array
23+
- Two Pointers
24+
- Binary Search
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
from typing import List
2+
3+
4+
def two_sum_less_than_k(nums: List[int], k: int) -> int:
5+
"""
6+
Finds the maximum sum of two elements in a given list of numbers that is less than k.
7+
Uses binary search to achieve a time complexity of O(n log n) and find the maximum sum of two elements
8+
that is less than k. It takes the nums array and the target value k as input.
9+
Args:
10+
nums (List[int]): A sorted list of integers
11+
k int: The target value to search for
12+
Returns:
13+
The maximum sum of two elements that is less than k
14+
"""
15+
max_sum = -1
16+
17+
# sort the numbers in ascending order to facilitate binary search
18+
nums.sort()
19+
20+
for x in range(len(nums)):
21+
# find the maximum sum of two elements that is less than k, with the first element being nums[x]
22+
y = search(nums, k - nums[x], x + 1)
23+
if y > x:
24+
# update max_sum with the maximum sum found so far
25+
max_sum = max(max_sum, nums[x] + nums[y])
26+
27+
return max_sum
28+
29+
30+
def search(nums: List[int], target: int, start: int) -> int:
31+
"""
32+
Searches for a number that is less than the target in a sorted list of numbers.
33+
Uses binary search to achieve a time complexity of O(log n) and find the index j such that the sum
34+
nums[i]+nums[j] < k. It takes the nums array, target value, and the start range of the search as input.
35+
Args:
36+
nums (List[int]): A sorted list of integers
37+
target int: The target value to search for
38+
start int: The starting index of the search range
39+
Returns:
40+
The index of the number that is less than the target, or -1 if no such number is found
41+
"""
42+
left, right = start, len(nums) - 1
43+
result = -1
44+
45+
while left <= right:
46+
# calculate the midpoint of the search range
47+
mid = (left + right) // 2
48+
if nums[mid] < target:
49+
# update result to mid and move left to mid + 1 to look for larger values.
50+
result = mid
51+
left = mid + 1
52+
else:
53+
# move right to mid - 1 to check smaller values.
54+
right = mid - 1
55+
56+
return result
51.6 KB
Loading
56.5 KB
Loading
56 KB
Loading
55.4 KB
Loading
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import unittest
2+
from . import two_sum_less_than_k
3+
4+
5+
class TwoSumLessKTestCase(unittest.TestCase):
6+
def test_1(self):
7+
"""numbers = [4,2,11,2,5,3,5,8], target = 7"""
8+
numbers = [4,2,11,2,5,3,5,8]
9+
target = 7
10+
expected = 6
11+
actual = two_sum_less_than_k(numbers, target)
12+
self.assertEqual(expected, actual)
13+
14+
def test_2(self):
15+
"""numbers = [10,20,30], target = 15"""
16+
numbers = [10, 20, 30]
17+
target = 15
18+
expected = -1
19+
actual = two_sum_less_than_k(numbers, target)
20+
self.assertEqual(expected, actual)
21+
22+
def test_3(self):
23+
"""numbers = [34,23,1,24,75,33,54,8], k = 60"""
24+
numbers = [34,23,1,24,75,33,54,8]
25+
k = 60
26+
expected = 58
27+
actual = two_sum_less_than_k(numbers, k)
28+
self.assertEqual(expected, actual)
29+
30+
def test_4(self):
31+
"""numbers = [5,5,5,5,5,5], k = 15"""
32+
numbers = [5,5,5,5,5,5]
33+
k = 15
34+
expected = 10
35+
actual = two_sum_less_than_k(numbers, k)
36+
self.assertEqual(expected, actual)
37+
38+
def test_5(self):
39+
"""numbers = [1,2,3,4,5], k = 3"""
40+
numbers = [1,2,3,4,5]
41+
k = 3
42+
expected = -1
43+
actual = two_sum_less_than_k(numbers, k)
44+
self.assertEqual(expected, actual)
45+
46+
47+
if __name__ == "__main__":
48+
unittest.main()

0 commit comments

Comments
 (0)