RSS Amplifier

SWE Prep · Mar 7, 2021

Find the Distance Value Between Two Arrays

0
Sign in to vote or save

Sameer Khoja · SWE Prep

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

No posts

Read the original on sweprep.substack.com

Comments

Nothing yet. Say the first thing.

    Sign in to join the conversation.