#C1564. Minimum Absolute Difference Between Two Arrays
Minimum Absolute Difference Between Two Arrays
Minimum Absolute Difference Between Two Arrays
You are given two arrays a
and b
of length n. Your task is to find the minimum absolute difference |a[i] - b[j]| for any pair of indices i and j, where 0 ≤ i, j < n
.
Problem Details:
The two arrays are provided as input in the following format:
- The first line contains an integer n (1 \leq n \leq 10^5), denoting the number of elements in each array.
- The second line contains n space-separated integers representing the array
a
(-10^9 \leq a_i \leq 10^9). - The third line contains n space-separated integers representing the array
b
(-10^9 \leq b_i \leq 10^9).
Your program should output a single integer which is the minimum absolute difference between any element in a
and any element in b
.
To achieve an efficient solution, consider sorting both arrays and then using a two-pointer technique to find the minimum difference.
inputFormat
Input Format:
- The first line contains an integer n (1 \leq n \leq 10^5).
- The second line contains n space-separated integers representing the array
a
(-10^9 \leq a_i \leq 10^9). - The third line contains n space-separated integers representing the array
b
(-10^9 \leq b_i \leq 10^9).
All input is read from stdin.
outputFormat
Output Format:
Print a single integer representing the minimum absolute difference between any pair of the elements from arrays a
and b
.
The output should be written to stdout.
## sample3
1 3 15
8 18 2
1
</p>