Problem · Easy · 15 min
Spend the whole budget
- Easy
- Arrays
- Hash maps
A shop lists the price of each item. You have a budget. You want to buy exactly two different items, and their prices must add up to the budget.
Write findPair(prices, budget). It returns an array of two positions [i, j] with i < j, such that prices[i] + prices[j] equals budget.
- Every test has exactly one pair of positions that works.
- The list can hold the same price more than once. The two positions must be different.
- Prices and the budget are whole numbers. They can be negative or zero.
Examples
| Call | Result |
|---|---|
findPair([2, 7, 11, 15], 9) |
[0, 1] |
findPair([4, 1, 6, 3], 9) |
[2, 3] |
findPair([5, 5], 10) |
[0, 1] |
Tasks
2 to do
Task 1, not checked yet. Make
findPairpass the example tests.Task 2, not checked yet. Make
findPairpass the hidden tests.
Task 1 of 2
Make findPair pass the example tests.