Skip to the code
cf.
+10 XP

#6 Best Coupon Pair Under Budget

easy

A shop's checkout page offers a deal: pick exactly two different items from prices and pay their total. The shopper has budget to spend.

Write bestPair(prices, budget) that returns the largest total of two different items (two different indexes) that is at most budget. If no pair fits, return -1.

Example 1

Input: prices = [30,45,10,25], budget = 60
Output: 55

45 + 10 and 30 + 25 both make 55, the largest total that is at most 60.

Example 2

Input: prices = [50,60], budget = 100
Output: -1

The only pair costs 110, which is over budget.

Constraints

  • 0 <= prices.length <= 1000
  • 1 <= prices[i] <= 10^6
  • 1 <= budget <= 2 * 10^6
Saved in this browser

Run tries your code on the examples. Submit runs every test, including hidden edge cases, and solves the problem when they all pass.

Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).