Practice

Problem 3 of 12

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

  1. Task 1, not checked yet. Make findPair pass the example tests.

  2. Task 2, not checked yet. Make findPair pass the hidden tests.

Task 1 of 2

Make findPair pass the example tests.

All tasks

main.js

Ctrl+Enter runs your code. Ctrl+Shift+Enter runs all the tests. Press Esc, then Tab, to leave the editor.

Output

Press Run to see your result

Your output shows here. Press Run to try your code.