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.
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.
Input: prices = [3,8,12,20], card = 6 Output: []
No two different prices add up to 6.
0 <= prices.length <= 2000001 <= prices[i], card <= 10^9prices is sorted ascending with no repeats.Run tries your code on the examples. Submit runs every test, including hidden edge cases and big inputs that check your code is fast enough, and solves the problem when they all pass.
Ctrl + Enter runs, Ctrl + Shift + Enter submits (⌘ on a Mac).
I'm here to help with #97 Gift Card Price Pair. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.