Find two elements with minimum difference in two arrays
Problem: Given two arrays, find two elements (one in each array) such that their difference is minimum
Solution : Sort both the arrays
Then follow an algorithm like this:
// Start at the beginning of both the arrays
int i=0;
int j=0;
// Initialize minDiff to maxvalue
int minDiff = Integer.MAX_VALUE;
while (i < arr1.length && j < arr2.length)
{
int diff = Math.abs(arr1[i]  arr2[j]);
if (diff < minDiff)
minDiff = diff;
// Move the smaller array's index forward
if (arr1[i] < arr2[j])
i++;
else
j++;
}
