Two Sum Less Than K - Problem

Imagine you're at a casino with a limited budget! ๐ŸŽฐ Given an array nums of integers and an integer k, you need to find two different numbers whose sum is as close to k as possible, but strictly less than k.

Your task is to return the maximum sum such that there exists i < j with nums[i] + nums[j] = sum and sum < k. If no such pair exists, return -1.

Example: If nums = [34,23,1,24,75,33,54,8] and k = 60, you want to find the pair that gives you the highest sum without going over 60. The answer would be 58 (from 34 + 24).

Input & Output

example_1.py โ€” Basic Case
$ Input: nums = [34,23,1,24,75,33,54,8], k = 60
โ€บ Output: 58
๐Ÿ’ก Note: We can choose the pair (34, 24) which gives us 34 + 24 = 58 < 60. This is the maximum possible sum less than 60.
example_2.py โ€” Small Array
$ Input: nums = [10,20,30], k = 15
โ€บ Output: -1
๐Ÿ’ก Note: No pair of numbers can sum to less than 15. The smallest possible sum is 10 + 20 = 30, which is โ‰ฅ 15.
example_3.py โ€” Edge Case
$ Input: nums = [1,1,1,1], k = 3
โ€บ Output: 2
๐Ÿ’ก Note: Any pair of 1's will give us 1 + 1 = 2 < 3. Since all pairs have the same sum, we return 2.

Visualization

Tap to expand
๐ŸŽฐ Casino Chip Selection (Two Pointers Approach)Budget: $60 | Find two chips with maximum total value < $60$1$8$23$24$33$34$54$75LEFTRIGHTBudget Line: $60Step-by-Step Process:1. $1 + $75 = $76 > $60 โ†’ Move RIGHT pointer left2. $1 + $54 = $55 < $60 โœ“ โ†’ Update max, move LEFT right3. $8 + $54 = $62 > $60 โ†’ Move RIGHT pointer left4. $8 + $34 = $42 < $60 โœ“ โ†’ Update max, continue...5. Eventually find: $34 + $24 = $58 < $60 โœ“๐ŸŽฏ Maximum Valid Combination: $34 + $24 = $58Time: O(n log n) | Space: O(1) | Approach: Sort + Two Pointers
Understanding the Visualization
1
Sort your chips by value
Arrange all chips from lowest to highest value for systematic selection
2
Use two-ended selection
Start with cheapest and most expensive chips available
3
Adjust selection intelligently
If total is under budget, try more expensive cheap chip. If over budget, try cheaper expensive chip
4
Track best combination
Remember the highest valid combination found during the process
Key Takeaway
๐ŸŽฏ Key Insight: Sorting enables intelligent navigation - when sum is too high, we need a smaller number (move right pointer left), when sum is too low, we need a larger number (move left pointer right). This systematic approach guarantees we find the optimal solution efficiently.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(n log n)

Sorting takes O(n log n), and the two-pointer traversal takes O(n)

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using constant extra space (if we sort in-place)

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค nums.length โ‰ค 100
  • 1 โ‰ค nums[i] โ‰ค 1000
  • 1 โ‰ค k โ‰ค 2000
  • Must find exactly two different elements (i < j)
Asked in
Amazon 45 Google 38 Microsoft 32 Meta 28
58.2K Views
Medium Frequency
~15 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen