1385 - Find the Distance Value Between Two Arrays (Easy)
Problem Link
https://leetcode.com/problems/find-the-distance-value-between-two-arrays
Problem Statement
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:
Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2
Output: 2
Explanation:
For arr1[0]=4 we have:
|4-10|=6 > d=2
|4-9|=5 > d=2
|4-1|=3 > d=2
|4-8|=4 > d=2
For arr1[1]=5 we have:
|5-10|=5 > d=2
|5-9|=4 > d=2
|5-1|=4 > d=2
|5-8|=3 > d=2
For arr1[2]=8 we have:
|8-10|=2 <= d=2
|8-9|=1 <= d=2
|8-1|=7 > d=2
|8-8|=0 <= d=2Example 2:
Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3
Output: 2Example 3:
Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6
Output: 1Constraints:
1 <= arr1.length, arr2.length <= 500-1000 <= arr1[i], arr2[j] <= 10000 <= d <= 100
Approach 1: Simulation
The most direct way is to check every pair . For each value in , we test whether all values in are farther than away from it. If that condition holds, then this value contributes to the final answer.
The time complexity is , which is still acceptable because both arrays have length at most .
class Solution:
def findTheDistanceValue(self, arr1: list[int], arr2: list[int], d: int) -> int:
return sum(
all(abs(v1 - v2) > d for v2 in arr2)
for v1 in arr1
)Approach 2: Binary Search
We only care whether there exists a value in within the interval for each in . After sorting , we can use binary search to quickly locate the first candidate that is at least .
Let . This gives the first index whose value is not smaller than . If is already out of bounds, then no such value exists. Otherwise, we only need to check : if , then even the first possible candidate is still too large, so there is no value in within distance from .
Sorting costs , and each query costs , so the total time complexity is .
from bisect import bisect_left
class Solution:
def findTheDistanceValue(self, arr1: list[int], arr2: list[int], d: int) -> int:
arr2.sort()
return sum((i := bisect_left(arr2, x - d)) == len(arr2) or arr2[i] > x + d for x in arr1)Approach 3: Two Pointers
We can do even better after sorting both arrays. Suppose we process from left to right. For a current value , any value in smaller than can never help this or any later value, so we move pointer forward until is the first value that is not too small.
After that, there are only two possibilities:
- reaches the end of , so no candidate remains
- or is the first possible candidate, and if it is still greater than , then no value in lies inside
Because both arrays are sorted, pointer never moves backward. This makes the scanning part linear, so the overall complexity is due to sorting, followed by an sweep.
class Solution:
def findTheDistanceValue(self, arr1: list[int], arr2: list[int], d: int) -> int:
arr1.sort()
arr2.sort()
res = j = 0
for x in arr1:
while j < len(arr2) and arr2[j] < x - d: j += 1
if j == len(arr2) or arr2[j] > x + d: res += 1
return res