Loading...
Loading...
A customer wants two items whose prices add up to exactly their gift-card value. Given the prices in order and the value, find two different positions i < j with price[i] + price[j] equal to the value. Print the pair with the smallest j; if several i work for that j, print the smallest i. Positions start at 0.
i j, or NONE if no pair exists.longExample 1
Input
6 4 9 1 7 3 5 10
Output
1 2
At position 2 (price 1) the price 9 was already seen at position 1.
Example 2
Input
4 3 3 4 5 6
Output
0 1
Two equal prices can make a pair.
Try the problem first. Open one hint at a time.
Scan left to right. For each price, the price you need is target - price.
Look the needed price up in a map from price to its first position, then add the current price to the map.
Check the map before you add the current price, so an item is never paired with itself.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor