Magicsheet logo

Two Sum Less Than K

Easy
25%
Updated 8/1/2025

Two Sum Less Than K

What is this problem about?

The Two Sum Less Than K interview question is a classic variation of the fundamental two-sum problem. In this challenge, you are given an array of integers and a target value K. Your goal is to find two distinct elements in the array such that their sum is as large as possible but still strictly less than K. If no such pair exists, you typically return -1. This problem tests your ability to navigate constraints and optimize searching within a collection of numbers.

Why is this asked in interviews?

Companies like Amazon frequently use the Two Sum Less Than K coding problem to assess a candidate's understanding of array manipulation and optimization. It moves beyond the basic hash-map approach of the original Two Sum and requires a more nuanced strategy, often involving sorting. It evaluates whether you can move from a naive O(n2)O(n^2) solution to a more efficient O(nlogn)O(n \log n) approach, which is a critical skill for handling large-scale data in real-world software engineering.

Algorithmic pattern used

The most efficient Array, Sorting, Two Pointers interview pattern for this problem involves sorting the array first. Once the array is sorted, you use two pointers: one starting at the beginning (the smallest value) and one at the end (the largest value). By comparing their sum to K, you can systematically narrow down the search space. If the sum is less than K, you record it as a potential maximum and move the left pointer forward to try and find a larger sum. If the sum is greater than or equal to K, you move the right pointer backward to reduce the total.

Example explanation

Imagine you have an array [10, 20, 30, 5, 15] and K = 35.

  1. First, sort the array: [5, 10, 15, 20, 30].
  2. Place pointers at 5 (left) and 30 (right). Sum = 35. Since 35 is not less than 35, move the right pointer left.
  3. Now pointers are at 5 and 20. Sum = 25. This is less than 35. Update maximum found to 25. Move left pointer right.
  4. Pointers at 10 and 20. Sum = 30. This is less than 35 and larger than 25. Update maximum to 30. Move left pointer right.
  5. Pointers at 15 and 20. Sum = 35. Not less than 35. Move right pointer left.
  6. The pointers meet, and the best result found is 30.

Common mistakes candidates make

One common pitfall is forgetting to sort the array, which makes the two-pointer approach impossible. Another mistake is using the same element twice, which violates the "distinct elements" rule. Candidates also sometimes struggle with the "less than K" constraint, accidentally including sums that are equal to K. Finally, failing to handle the case where no pair exists (returning -1) is a frequent oversight.

Interview preparation tip

When practicing the Two Pointers interview pattern, always ask yourself if sorting the data provides a benefit. Many array-based problems that involve searching for pairs or triplets become significantly simpler once the input is ordered. Master the logic of when to move each pointer, as this is the core of the optimization.

Similar Questions