Find the Distance Value Between Two Arrays

Lessons Learned: TreeSet, Array Manipulation

Question: Given two integer arrays arr1 and arr2, and the integer dreturn the distance value between the two arrays.

The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.

While this question may seem daunting at first, we can simply utilize a TreeSet to add values from the second array into. Then we can get a lower and upper bound (minus d, plus d) of each number in arr1, and then see if there are any values in arr2 that are within this lower and upper bound.

See Solution