Loading...
Loading...
Loading...
A shop wants two items in a price list that add up to a given budget. Positions start at 0.
Scan the list from left to right. For the first position j whose price completes a pair with an earlier position i, print i j. If no pair exists, print -1.
i j with i < j, or -1.Example
Input
2 7 11 15 9
Output
0 1
2 and 7 add up to 9.
Try the problem first. Open one hint at a time.
For each price you need target minus price to have appeared earlier. Remember earlier prices in a dictionary with their positions.
Check the dictionary first, then store the current price. Storing first would let a price pair with itself.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor