615. Two Sum Less Than K

Given an array nums of integers and an integer k, return the maximum sum such that there exists i < j with nums[i] + nums[j] = sum and sum < k. If no such i, j exist, return -1.

Example 1:

Input: nums = [20, 15, 35, 40, 10], k = 50

Output: 45

Explanation: We can use 35 and 10 to get 45, which is less than 50.

Example 2:

Input: nums = [5, 1, 8, 12, 18], k = 10

Output: 9

Explanation: The pair 1 + 8 = 9 is the maximum sum below 10.

Now Your Turn!

Pick the correct output for the given input

Input: nums = [7, 3, 2, 9, 14], k = 5

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= nums.length <= 100
  • 1 <= nums[i] <= 1000
  • 1 <= k <= 2000

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

0
class Solution {
public:
int twoSumLessThanK(vector<int>& nums, int k) {
// Your code goes here
}
};
 
Test Case

Input:

K
Nums