SWE Prep

Share this post
Find the Distance Value Between Two Arrays
sweprep.substack.com

Find the Distance Value Between Two Arrays

Lessons Learned: TreeSet, Array Manipulation

Sameer Khoja
Mar 7, 2021
Share this post
Find the Distance Value Between Two Arrays
sweprep.substack.com

Question: Given two integer arrays arr1 and arr2, and the integer d, return 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

Share this post
Find the Distance Value Between Two Arrays
sweprep.substack.com
TopNew

No posts

Ready for more?

© 2022 Sameer Khoja
Privacy ∙ Terms ∙ Collection notice
Publish on Substack Get the app
Substack is the home for great writing