Find the Distance Value Between Two Arrays - Problem
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.
In other words, count how many elements in arr1 are more than distance d away from all elements in arr2.
Input & Output
Example 1 — Basic Case
$
Input:
arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
›
Output:
2
💡 Note:
For arr1[0]=4: closest in arr2 is 1, |4-1|=3 > 2 ✓. For arr1[1]=5: closest is 1, |5-1|=4 > 2 ✓. For arr1[2]=8: found 8 in arr2, |8-8|=0 ≤ 2 ✗. Result: 2 elements count.
Example 2 — All Elements Count
$
Input:
arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
›
Output:
2
💡 Note:
For arr1[0]=1: closest in arr2 is -3, |1-(-3)|=4 > 3 ✓. For arr1[1]=4: closest is 6, |4-6|=2 ≤ 3 ✗. For arr1[2]=2: closest is -3, |2-(-3)|=5 > 3 ✓. For arr1[3]=3: closest is 6, |3-6|=3 ≤ 3 ✗. Result: 2 elements count.
Example 3 — No Elements Count
$
Input:
arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
›
Output:
1
💡 Note:
Only arr1[2]=100 is far enough from all arr2 elements. The others have at least one arr2 element within distance 6.
Constraints
- 1 ≤ arr1.length, arr2.length ≤ 500
- -1000 ≤ arr1[i], arr2[j] ≤ 1000
- 0 ≤ d ≤ 100
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code