bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 1099. Two Sum Less Than K

1099. Two Sum Less Than K

Leetcode Solutions

Approach: Two Pointers

  1. Sort the array nums.
  2. Initialize two pointers, left set to 0 and right set to len(nums) - 1.
  3. Initialize answer to -1 to keep track of the maximum sum found that is less than k.
  4. While left is less than right: a. Calculate the sum of nums[left] and nums[right]. b. If the sum is less than k, update answer with the maximum of answer and the sum, then increment left. c. If the sum is greater or equal to k, decrement right.
  5. Return answer.
UML Thumbnail

Approach: Brute Force

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...