Skip to the code
cf.
+10 XP

#27 Pair of Gift Cards

easy

A shopper has a gift card worth exactly total and wants two different items from prices that use it up exactly.

Write giftCardPair(prices, total) that returns the indexes [i, j] of the two items, with i < j. Exactly one pair works for every test.

Example 1

Input: prices = [25,40,10,60], total = 50
Output: [1,2]

40 + 10 = 50.

Example 2

Input: prices = [15,35,35], total = 70
Output: [1,2]

Two different items can have the same price.

Constraints

  • 2 <= prices.length <= 10^5
  • -10^9 <= prices[i], total <= 10^9
  • Exactly one pair of indexes adds up to total.
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).