I came across a post Find the minimum difference between two arrays
and using the concept I tried to solve a problem of SPOJ – http://www.spoj.pl/problems/ACPC11B but strangely I got WA (Wrong Answer) !!!!
I then tried the simple way..
using two for loops..
Calculating the difference between each and every element …
Then I got AC. !!!!
Can anyone please tell me why this method
if (|A[i+1] – B[j]| < |A[i] – B[j+1]|) then increment i, otherwise increment j fails in this case ????
EDIT: I forgot to mention that when I implemented the first method I have already performed qsort on both the arrays…
Also in the SPOJ question, indexes of the elements whose difference is minimum are not required. Only minimum difference is required !!!
Here’s what I think should do the trick (based on your code), basically your code didn’t check 2 cases:
First when the minimum is between the 2 first values in the arrays.
Second is when we’ve checked every value in one array but still have values in the second. (Consider the case a[0,1,2], b[-1,-2,-3,-5,-6,2])