Loading...
Loading...
An online shop lists n item prices in non-decreasing order (each price is at least the one before it). You have a gift card worth exactly B rupees and want to buy two different items whose prices add up to exactly B.
Print YES if such a pair exists, otherwise print NO.
YES or NO.Example 1
Input
5 15 2 4 7 11 15
Output
YES
4 + 11 = 15.
Example 2
Input
3 10 1 2 3
Output
NO
Try the problem first. Open one hint at a time.
The prices are sorted, so put one pointer at the cheapest item and one at the most expensive.
If the sum is too small, move the left pointer right. If it is too big, move the right pointer left.
Stop when the pointers meet: left < right makes sure the two items are different.
B can be 4 × 10^9, which does not fit in an int. Read B and add prices as long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor