Find the Distance Value Between Two Arrays

Given two integer arrays arr1 and arr2 and an integer d, return the distance value: the number of elements arr1[i] such that no element arr2[j] satisfies |arr1[i] - arr2[j]| <= d.

30:00

Find the Distance Value Between Two Arrays
easy
Free written solution
Topics
Companies

Given two integer arrays arr1 and arr2 and an integer d, return the distance value: the number of elements arr1[i] such that no element arr2[j] satisfies |arr1[i] - arr2[j]| <= d.

Example 1:
Input: {"arr1":[4,5,8],"arr2":[10,9,1,8],"d":2}
Output: 2
Constraints:
  • 1≤arr1.length,arr2.length≤5001 \leq arr1.length, arr2.length \leq 500

  • −1000≤arr1[i],arr2[j]≤1000-1000 \leq arr1[i], arr2[j] \leq 1000

  • 0≤d≤1000 \leq d \leq 100

Test Cases
Input (Stdin)
Expected Output (Stdout)
Execution Result

Run code to see results.

Input
arr ={"arr1":[4,5,8],"arr2":[10,9,1,8],"d":2}

Sort arr2 to [1, 8, 9, 10] so one binary search can find the arr2 value closest above a - d.

arr1 (is each value far from all of arr2?)

count = 0

4
5
8

number line

-2

12

● arr2 value

● arr2 value inside the window

▭ |a - x| ≤ d

arr2 sorted: find the first value ≥ a - d

1
8
9
10
Variables
VariableValue
d2
DepthFunction Call
Stack empty
0/26