


N = 3, arr1 = [10, 20, 30]
M = 2, arr2 = [17, 15]
The smallest difference pair is (20, 17) with an absolute difference of 3. So, the answer is 3.
Both the arrays are unsorted, and all array elements are non-negative integers.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The first line of each test case contains two space-separated integers, 'N' and 'M', where 'N' and 'm' are the sizes of array 'arr1' and 'arr2', respectively.
The second line of each test case contains 'N' space-separated integers denoting the elements of array 'arr1'.
The third line of each test case contains 'M' space-separated integers denoting the elements of array 'arr2'.
For each test case, return the smallest absolute difference.
You do not need to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N, M <= 1000
0 <= arr1[i], arr2[i] <=10^6
Time Limit: 1 second
We can use two nested loops to iterate through all the possible ‘M * N’ pairs of values to calculate the smallest absolute difference among all the pairs.
Sort arrays 'arr1' and 'arr2' in ascending order. Let ‘i’ be the pointer to the first element of 'arr1', ‘j’ be the pointer to the first element of 'arr2', and ‘minDiff’ store the final answer.
Store the current difference i.e. ‘abs(arr1[i] - arr2[j])’ in ‘minDiff’. Assuming that ‘arr2[j]’ is greater than ‘arr1[i]’, then incrementing ‘j’ will only increase the difference between ‘arr2[j]’ and ‘arr1[i]’. Therefore we need to increment ‘i’, and update ‘minDiff’ if it’s greater than the new difference. We have to iterate until one of the arrays is completely traversed. For each iteration, update ‘minDiff’ and increment the index with the smaller array value.