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.