We’re given two arrays and we need to find the smallest difference, from two numbers in each array
Essentially the closes numbers from both arrays
Solution
To solve this we can start off by sorting the array
With sorting the array we can take advantage of some properties of a sorted array.
We can set two pointers two iterate through the two arrays. However as we iterate we calculate the difference and keep track of this difference. If we see that the left pointer is less than the right pointer then increasing the left pointer will yield a smaller difference, if the right pointer is less than the right pointer then we can increase the right pointer. we keep doing this until we’ve reach the end with the smallest possible difference.
The time complexity is O(n(log n) + M log(M)) because we are sorting both arrays
The space complexity O(1) since we are just storing the smallest difference