Two warehouses each keep a sorted (non-decreasing) log of shipment values: log A has n values and log B has m values. You must pick exactly one value from A and one value from B; among all such cross pairs, find the minimum possible absolute difference between the pair's sum and a target T.
Line 1: three integers n, m, and T.
Line 2: n space-separated integers, sorted non-decreasing — log A.
Line 3: m space-separated integers, sorted non-decreasing — log B.
A single integer: the minimum value of |a + b - T| over all a in A and b in B.
Example 1
Input
3 3 10 1 4 6 2 5 9
Expected
0
Explanation
The pair 1 + 9 = 10 matches the target exactly, so the minimum difference is 0.
Example 2
Input
2 2 0 -5 3 -2 4
Expected
1
Explanation
The pairs -5+4=-1 and 3+-2=1 both give |sum| = 1, which is the best achievable (no pair sums to exactly 0).
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 →