Skip to the code
cf.
+10 XP

#97 Gift Card Price Pair

easy

A shop's checkout suggests two different products that use up a gift card exactly. The product prices are in prices, sorted from cheapest to most expensive, and no two prices are the same.

Write findPricePair(prices, card) that returns the indexes [i, j] (with i < j) of two products whose prices add up to exactly card. If several pairs work, return the one whose cheaper product is the cheapest. If no pair works, return [].

Use O(1) extra memory.

Example 1

Input: prices = [5,10,15,25,40], card = 50
Output: [1,4]

10 + 40 = 50. The pair 25 + 25 isn't allowed, because it's the same product twice.

Example 2

Input: prices = [3,8,12,20], card = 6
Output: []

No two different prices add up to 6.

Constraints

  • 0 <= prices.length <= 100000
  • 1 <= prices[i], card <= 10^9
  • prices is sorted ascending with no repeats.
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).