Two suppliers publish price catalogs, each already sorted in non-decreasing order. You want to find the closest match: choose one price from catalog A and one from catalog B so that the absolute difference between them is as small as possible, and report that smallest difference.
Line 1: two integers n and m, the sizes of catalog A and catalog B.
Line 2: n non-decreasing integers, catalog A.
Line 3: m non-decreasing integers, catalog B.
A single integer: the minimum possible value of |A[i] - B[j]|.
Example 1
Input
3 3 1 8 15 5 9 20
Expected
1
Explanation
The closest cross-catalog pair is 8 and 9, differing by 1, which is the minimum.
Example 2
Input
2 2 4 12 12 30
Expected
0
Explanation
Catalog A's 12 matches catalog B's 12 exactly, so the minimum difference is 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 →