Matching Receipts
You paid for everything on a trip with a friend. Back home, your friend sends money for exactly two of your receipts, with only the total: target.
You have the receipt amounts in the order you paid them. Find the two receipts your friend paid for.
Your task
Write findPair(amounts, target). Return the positions (indexes) of the two receipts whose amounts add up to target, in either order.
Each receipt can be used once. There is always exactly one correct pair.
findPair(amounts: int[], target: int) → int[] Any order of the returned list is accepted.
Examples
amounts = [40, 25, 75, 15] target = 100
[1, 2]
25 + 75 = 100, at positions 1 and 2. No other pair works: 40 would need a 60, and 15 an 85.
amounts = [5, 5] target = 10
[0, 1]
Two receipts can share an amount. 5 + 5 = 10, using positions 0 and 1. Using position 0 twice would not count.
Constraints
- 2 ≤ amounts.length ≤ 10,000
- 1 ≤ amounts[i] ≤ 1,000,000
- Exactly one pair of different positions adds up to target.
Start with brute force
Try every pair: for each position i, check every later j for amounts[i] + amounts[j] === target.
It is correct, but pairs grow with n²: 10,000 receipts make about 50 million. The slow part is the inner loop searching the whole list for a partner every time.
Hints
0 of 4 shown- Hint 1
Pick one receipt, say the 25 in the first example. If it is part of the answer, what must the other amount be?
- Hint 2
For each receipt
a, the partner must be exactlytarget - a. So at every receipt, ask whether you have already seentarget - aand at which position. - Hint 3
Which data structure answers "have I seen this value, and at what position?" in one step, without rescanning the list?
- Hint 4
Walk the list once, keeping a hash map from amount to position. At each receipt, look up
target - afirst; if it is there, you are done. If not, storeawith its position. Looking up before storing stops a receipt pairing with itself.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
The brute force searches for a partner from scratch for every receipt. Instead, remember what you have passed, so finding a partner is one lookup.
Trace [40, 25, 75, 15] with target 100:
- At position 0 the amount is 40, so the partner would be 60. The map does not have 60, so store
40 → 0. - At position 1 the amount is 25, so the partner would be 75. The map does not have 75, so store
25 → 1. - At position 2 the amount is 75, so the partner would be 25. The map has
25 → 1, so return[1, 2].
Look up first, then store. If you stored first, a receipt of 50 with target 100 would find itself.
function findPair(amounts, target) {
const seen = new Map(); // amount -> position
for (let i = 0; i < amounts.length; i++) {
const need = target - amounts[i];
if (seen.has(need)) return [seen.get(need), i];
seen.set(amounts[i], i);
}
return []; // never reached: there is always a pair
}def find_pair(amounts, target):
seen = {} # amount -> position
for i, a in enumerate(amounts):
need = target - a
if need in seen:
return [seen[need], i]
seen[a] = i
return [] # never reached: there is always a pairRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.