Ctrl K

Best Pair Under the Limit

Easy
A shop sells items priced at prices. You want to buy exactly two different items so that their total is **strictly less** than limit, while spending as much as possible. Return the largest such total. If no pair fits under the limit, return -1.
Example 1
prices=[40, 15, 70, 25, 55], limit=80 → 70

The best pair is 55 and 15, for 70. No pair lands on 71–79: 55 + 25 = 80 is no longer below the limit.

Example 2
prices=[30, 45], limit=60 → -1

The only pair costs 75, which is not below 60, so the answer is -1.

Constraints
  • 1 ≤ len(prices) ≤ 10⁵
  • 1 ≤ prices[i] ≤ 1000
  • 1 ≤ limit ≤ 2000
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsValues in Both Lists EasyShared Values With Repeats EasyTwice as Much EasyFar From Every Mark EasyPairs Below the Limit Easy
loading Python…
Input
prices=[40, 15, 70, 25, 55], limit=80
Expected
70

Run checks the first 3 tests in your browser. Submit checks all 100 tests on the server, hidden ones included.