Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/ikki-korsatkich-eng-yaqin-ikki-massiv

Closest pair from two arrays

Easy 1000 ms 128 MB 24%
Share

Two arrays sorted in non-decreasing order are given: $a$ of $n$ numbers and $b$ of $m$ numbers. If one number is taken from the first array and one from the second, what is the smallest possible absolute difference $|a_{i} - b_{j}|$?

Two sorted arrays: the two closest numbers are joined by a line, their difference is highlighted

Input

The first line contains two positive integers: $n$ and $m$.
The second line contains $n$ integers $a_{i}$.
The third line contains $m$ integers $b_{j}$.

Output

One integer — the smallest difference.

Note

$1 \le n, m \le 10^{5}$
$-10^{9} \le a_{i}, b_{j} \le 10^{9}$
both arrays are in non-decreasing order

Examples

Input #1
3 4
1 5 9
3 8 12 20
Output #1
1
Input #2
1 1
4
4
Output #2
0

Sign in to submit a solution

Signing up is free and takes a minute.