A thermostat controller picks two of its n sensor readings and adds them to estimate a setpoint. Given a target setpoint t, choose two distinct readings whose sum is as close to t as possible, and report how far off the best pair is — that is, the minimum value of |reading[i] + reading[j] - t| over all pairs i < j. The readings are not sorted.
Line 1: an integer n.
Line 2: n space-separated integers, the sensor readings.
Line 3: an integer t, the target setpoint.
A single integer: the minimum achievable |reading[i] + reading[j] - t|.
Example 1
Input
4 1 3 6 10 11
Expected
0
Explanation
Pair sums are 4,7,11,9,13,16. The sum 11 hits the target exactly, so the minimum gap is 0.
Example 2
Input
3 2 4 9 8
Expected
2
Explanation
Pair sums are 6, 11, 13; the closest to 8 is 6, off by 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →