Skip to content
Onsite

Two Sum Less Than K

EasyP3Coding

Given an array nums and an integer k, find the maximum value of nums[i] + nums[j] over pairs of different indices i < j such that the sum is strictly less than k. Return -1 if no such pair exists.

Example 1

Input
nums = [34,23,1,24,75,33,54,8], k = 60
Output
58

Example 2

Input
nums = [10,20,30], k = 15
Output
-1

Example 3

Input
nums = [5], k = 10
Output
-1

Constraints

  • 1 <= nums.length <= 10^4
  • 1 <= nums[i] <= 1000
  • 1 <= k <= 2000

Hints

Run checks the 3 visible tests, Submit all 11.