Find the minimum difference between two arrays [closed]
G开发者_JS百科iven 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.
精彩评论