Given an array nums of integers and an integer k, find the maximum sum such that there exists i < j with nums[i] + nums[j] = sum and sum < k. If no i and j exist satisfying this equation, return -1.
Example 1:
Input: nums = [1,3,6,7,9], k = 10
Output: 9
Explanation: The pairs (1,3), (1,6), (1,7), (1,9), (3,6), (3,7), (3,9), (6,7) have sums 4, 7, 8, 10, 9, 10, 12, 13.
Among these, 9 is the largest sum that is less than k = 10.
Example 2:
Input: nums = [34,23,1,24,75,33,54,8], k = 60
Output: 58
Explanation: The pair (34,24) has sum 58 which is less than 60.
Constraints:
1 <= nums.length <= 1001 <= nums[i] <= 10001 <= k <= 2000When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The brute force approach to this problem is like trying every possible pair of numbers. We will look at each combination to see if it meets our condition. We aim to find the largest sum of two numbers that is still smaller than the specified limit.
Here's how the algorithm would work step-by-step:
def two_sum_less_than_k_brute_force(numbers, limit):
largest_sum_smaller_than_limit = -1
# Iterate through each number in the list
for first_number_index in range(len(numbers)):
# For each number, iterate through the rest of the numbers
for second_number_index in range(first_number_index + 1, len(numbers)):
current_sum = numbers[first_number_index] + numbers[second_number_index]
# Check if the current sum is less than the limit
if current_sum < limit:
# Keep track of the largest sum that satisfies this condition
largest_sum_smaller_than_limit = max(largest_sum_smaller_than_limit, current_sum)
return largest_sum_smaller_than_limitThe best way to solve this problem is to first organize the numbers in ascending order. Then, use two pointers, one starting at the beginning and the other at the end, to efficiently find the pair that adds up to the largest sum less than the target value.
Here's how the algorithm would work step-by-step:
def two_sum_less_than_k(numbers, target):
numbers.sort()
left_pointer = 0
right_pointer = len(numbers) - 1
max_sum_less_than_target = -1
while left_pointer < right_pointer:
current_sum = numbers[left_pointer] + numbers[right_pointer]
# Update the result if current sum is less than target.
if current_sum < target:
max_sum_less_than_target = max(max_sum_less_than_target, current_sum)
# Try to find a larger sum.
left_pointer += 1
# We need a smaller sum.
else:
right_pointer -= 1
return max_sum_less_than_target| Case | How to Handle |
|---|---|
| Null or empty input array | Return 0 immediately, as no sum less than K is possible. |
| Input array with fewer than 2 elements | Return 0, as no pair can be formed. |
| All numbers in the array are greater than or equal to K | Return 0, as no sum less than K is possible. |
| Array contains only one unique number repeated multiple times | Ensure that the algorithm does not add the number to itself, unless K is greater than twice the number. |
| Large input array (scalability considerations) | Sorting the array upfront allows for a two-pointer approach with O(n log n) time complexity, avoiding quadratic complexity of naive solutions. |
| Negative numbers in the input array | The algorithm should correctly handle negative numbers and consider their contribution to the sum. |
| K is a very small negative number | The solution should correctly identify the largest possible sum less than a negative K, potentially being a large negative number itself. |
| Duplicate pairs exist | The two-pointer approach implicitly handles duplicate pairs by moving pointers and ensuring the calculated sum is the largest possible sum less than K. |