Find the minimum distance between two sorted lists where distance is the absolute difference between pair of elements
For Example:
int a[] = {2, 3, 5, 11, 18, 19, 20}; int b[]= {15, 24, 27, 29};
Output should be 3 (18-15)
Solution:
int minDist(int a[], int m, int b[], int n){
int i = 0,
j = 0,
min = INT_MAX,
cur;
while(i < m && j < n) {
cur = abs(a[i] - b[j]);
min = min < cur ? min : cur;
if(a[i] < b[j])
i++;
else
j++;
}
return min;
}
No comments:
Post a Comment