Skip to content
Algorithms for Beginners

Problems / Hashing and counting

Pair to target

easy

New here? How this page works
  1. Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
  2. Open Break it down. Answer the questions in your own words. This is your plan.
  3. Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
  4. Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
  5. Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
  6. Press Submit. It runs hidden tests too. Pass them all, and it's solved.

You have a gift card worth exactly budget, and you want to spend all of it on two different items.

Given a list of item prices and the budget, return the positions of the two items whose prices add up to exactly budget.

Positions start at 0, so the first item is at position 0. You can return the two positions in either order.

Examples

  1. Example 1

    Input
    prices = [3, 8, 5, 2], budget = 10
    Output
    [1, 3]

    8 (position 1) + 2 (position 3) = 10.

  2. Example 2

    Input
    prices = [4, 4], budget = 8
    Output
    [0, 1]

    Two different items can have the same price.

  3. Example 3

    Input
    prices = [1, 6, 9, 13], budget = 15
    Output
    [1, 2]

    6 + 9 = 15.

Rules

  • There is always exactly one pair of items that works.
  • You can't pick the same item twice, but two different items may have the same price.
  • prices has between 2 and 10,000 items. Prices can be negative or zero.
Words on this page, in plain English (2)Open if any word is new to you
Array (list)
A list of values kept in order, inside square brackets. Like a row of numbered lockers.
Index (position)
Where an item sits in a list. Counting starts at 0, not 1, so the first item is at position 0.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function pairToTarget(prices, budget) {
  // Return an array with two positions, like [0, 3].
}

Blank page fear? (uses your answers from Break it down; comments don’t run).

Results

Run the examples to check your code, then Submit to run every test.

Next steps in hashing and counting

  • Learn the hashing and counting pattern

    Remember what you have seen in a Map, so you never search for it twice.

  • Spot a repeat

    Easy. Check whether any raffle ticket number was printed more than once.

  • Letter twins

    Easy. Decide whether one word can be made by rearranging all the letters of another.

  • First lonely letter

    Easy. Find the position of the first letter that appears only once in a piece of text.

  • Group the twins

    Medium. Sort a list of words into groups, where every word in a group uses exactly the same letters.

  • Longest run

    Medium. Find the length of the longest run of consecutive numbers hidden in an unsorted list.