Skip to content
Algorithms for Beginners

Problems / Two pointers

Pair in a sorted list

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.

Like Pair to target, but this time the prices are sorted from smallest to largest.

Return the positions of the two different items whose prices add up to exactly budget, smaller position first.

Because the list is sorted, you can solve this using only a couple of extra variables, without a Map.

Examples

  1. Example 1

    Input
    prices = [1, 3, 4, 5, 9], budget = 10
    Output
    [0, 4]

    1 + 9 = 10.

  2. Example 2

    Input
    prices = [2, 5, 7, 11, 15], budget = 12
    Output
    [1, 2]

    5 + 7 = 12.

  3. Example 3

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

    Prices can be negative: -4 + 3 = -1.

Rules

  • prices is sorted from smallest to largest, and has between 2 and 100,000 items.
  • There is always exactly one pair that works.
  • Return [smaller position, larger position].
Words on this page, in plain English (5)Open if any word is new to you
Variable
A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
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.
Map
A lookup table: it stores pairs of a key and a value, and finds any key instantly. Like a coat check: hand over a ticket, get your coat straight back.
Sorted
In order, smallest to biggest (or A to Z). Many fast tricks only work on sorted lists.

Every word, explained simply

Your solution

JavaScript · Press Esc then Tab to leave the editor

function sortedPair(prices, budget) {
  // Return [smaller position, larger position].
}

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 two pointers

  • Learn the two pointers pattern

    Walk through a list from both ends (or at two speeds) at once.

  • Squash repeats

    Easy. Remove repeated values from a sorted list in place, keeping one of each.

  • Biggest tank

    Medium. Choose two walls that, with the ground between them, hold the most water.

  • Three to zero

    Medium. Find every different set of three numbers in a list that adds up to zero.

  • Mirror phrase

    Easy. Check whether a phrase reads the same backwards, ignoring spaces, punctuation and capitals.

  • Flip in place

    Easy. Reverse a list by swapping items inside it, without building a new list.