Solution: Two Sum Less Than K

Let’s solve the Two Sum Less Than K problem using the Sort and Search pattern.

Statement

Given an array of integers, nums, and an integer k, find the maximum sum of two elements in nums less than k. Otherwise, return 1−1 if no such pair exists.

Constraints:

  • 11\leq nums.length 100\leq100

  • 11\leq nums[i] 103\leq 10^3

  • ...