Ctrl K

Two Snacks on a Budget

Easy
A vending machine sells snacks with prices given in prices. You have budget coins. You want to buy exactly two snacks (two different positions in the array) without going below zero. Among all such purchases, pick the one that leaves you with the most money and return what is left. If you cannot afford any two snacks, buy nothing and return budget.
Example 1
prices=[7, 3, 5, 4], budget=10 → 3

The cheapest pair costs 3 + 4 = 7, leaving 3.

Example 2
prices=[6, 8, 9], budget=12 → 12

Even the cheapest pair costs 14, which is over budget, so you keep all 12.

Example 3
prices=[5, 5], budget=10 → 0
Constraints
  • 2 ≤ len(prices) ≤ 50
  • 1 ≤ prices[i] ≤ 100
  • 1 ≤ budget ≤ 200
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsCake Slices for Guests EasyStrength of the Weaker Partner EasyTriangle From Sticks EasySign Flips for the Largest Sum EasyPack the Backpack Easy
loading Python…
Input
prices=[7, 3, 5, 4], budget=10
Expected
3

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