Given two sorted arrays, A and B, find i,j for which |A[i] - B[j]| is minimum.
Since the arrays are sorted, you can pass through them with 2 pointers (one for each array). If |A[i+1] - B[j]| < |A[i] - B[j+1]|
then increment i
, otherwise increment j
. Continue until you've reached the end of one of the arrays. Keep track of the minimal indexes as you go.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With