Easy
You are given two integer arrays arr1 and arr2, and an integer d.
An element arr1[i] is called far when every element arr2[j] satisfies |arr1[i] - arr2[j]| > d. In other words, no element of arr2 lies within distance d of it.
Return the distance value, which is the number of far elements in arr1.
Example 1:
Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
Output: 2
Explanation:
- 4 is far. Its nearest value in
arr2is 1, and|4 - 1| = 3, which is greater thand = 2. - 5 is far. Its nearest value in
arr2is 8, and|5 - 8| = 3, which is greater thand = 2. - 8 is not far, because
|8 - 8| = 0is at mostd = 2. - Two elements are far, so the distance value is 2.
Example 2:
Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
Output: 2
Explanation:
- 1 is far. Its nearest value in
arr2is -3, and|1 - (-3)| = 4, which is greater thand = 3. - 2 is far. Its nearest value in
arr2is 6, and|2 - 6| = 4, which is greater thand = 3. - 4 is not far, because
|4 - 6| = 2is at mostd = 3. - 3 is not far, because
|3 - 6| = 3is at mostd = 3. - Two elements are far, so the distance value is 2.
Example 3:
Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
Output: 1
Explanation:
- 100 is far. Its nearest value in
arr2is 10, and|100 - 10| = 90, which is greater thand = 6. - 2 is not far, because
|2 - (-2)| = 4is at mostd = 6. - 1 is not far, because
|1 - (-2)| = 3is at mostd = 6. - 3 is not far, because
|3 - 7| = 4is at mostd = 6. - One element is far, so the distance value is 1.
Constraints:
1 <= arr1.length, arr2.length <= 500-1000 <= arr1[i], arr2[j] <= 10000 <= d <= 100