Loading...
Loading...
Loading...
From a list of costs, pick three different items so that their total is as close as possible to a target. Print that total. If two totals are equally close, print the smaller one.
Example
Input
-1 2 1 -4 1
Output
2
-1 + 2 + 1 = 2 is the closest total.
Try the problem first. Open one hint at a time.
Sort, fix one value, then run a closest-pair search on the rest.
Compare the tuple (distance, total) to handle ties, and stop early on an exact match.
Write your Python code, run it on the examples and submit it to hidden tests.
Solve in the editor