Daily Archives: December 22, 2015

Find min distance of two list

Given two sorted list, find the min distance of any 2 elements in 2 lists.
For example,
{1, 10, 20} and {5, 15, 21} min=21-20=1
{1, 10} and {15, 21} min=15-10=5

Solution 1 is to use merge sort. Compare abs(arr1[i]-arr[2] ) and min.

Solution 2 is that it doesn’t compare in each merge. It only compares when merge list changes. Here I say merge list is where min element from in each step.

Take arr1=[1, 2, 3, 4, 8], arr2=[7, 8, 9, 10] as example, the process is like below:

check my 2 solutions on github: link