Loading...
Loading...
Loading...
A buyer has a target spend. From a list of item costs, pick two different items so that their total is as close as possible to the target. Print that total. If two totals are equally close, print the smaller one.
Example
Input
10 22 28 29 30 40 54
Output
52
22 + 30 = 52 and 28 + 29 = 57 are both close; 52 is 2 away and 57 is 3 away.
Try the problem first. Open one hint at a time.
Sort, then use two pointers from both ends.
Keep the best total seen so far, comparing by distance first and by value for ties.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor