Find the Distance Value Between Two Arrays

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.

Example 1
Inputarr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
Output2
Only 4 and 5 in arr1 have no element in arr2 within distance 2, while 8 is within distance 2 of elements in arr2.
Example 2
Inputarr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
Output2
There are 2 elements in arr1 that do not have any element in arr2 within distance 3.

Constraints

  • 1 <= arr1.length, arr2.length <= 500
  • -1000 <= arr1[i], arr2[j] <= 1000
  • 0 <= d <= 100

Asked at 6 companies

Practice questions like this out loud in a mock coding interview with a live AI interviewer. First one free.

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, what to keep, what's wrong, and the complexity of your solution.

Sign in to evaluate