#62 · Two pointers · Intermediate · 60 XP · about 10 min
A shop lists its prices from cheapest to dearest, and some prices repeat. Write pair_sum_sorted(prices, target) that finds two different items whose prices add up to exactly target and returns their prices as [cheaper, dearer]. If several pairs work, return the one whose cheaper item is cheapest. If no pair works, return [].
Two items can have the same price, but one item cannot be used twice.
pair_sum_sorted([2, 3, 5, 6, 8, 9], 11) # returns [2, 9] pair_sum_sorted([1, 4, 4, 9], 8) # returns [4, 4] pair_sum_sorted([3, 4, 9], 8) # returns []
In the first example 2 + 9, 3 + 8 and 5 + 6 all make 11, and the pair with 2 has the cheapest item. In the last example 4 + 4 would make 8, but there is only one item priced 4.
0 <= len(prices) <= 100,0000 <= prices[i], target <= 10^9prices is sorted in ascending orderReady?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.