Category Archives: Matrix

Find smallest different in given two arrays;

Find smallest different in given two arrays

Algorithm:
  1. Sort both arrays
  2. Set minDiff = INFINITY
  3. Set a1Counter = 0 and a2Counter = 0
  4. Iterate till has array has not reached end
    1. If abs(A1[a1Counter] – A2[a2Counter]) < minDiff then
      1. Set minDiff = abs(A1[a1Counter] – A2[a2Counter]
    2. if A1[a1Counter] < A2[a2Counter] then
      1. Set a1Counter = a1Counter + 1
    3. else
      1. Set a2Counter = a2Counter + 1
  5. return minDiff

Latest Source Code:
Github: SmallestDifferenceInTwoArrays.java


Output:

Minimum difference: 3
Minimum difference: 1