Statementâ–¼
You are given two integer arrays, arr1
and arr2
, along with an integer d
. Your task is to find and return the distance value between these arrays.
Note: The distance value is defined as the count of elements inÂ
arr1
 for which there is no element inÂarr2
 such that∣arr1[i]−arr2[j]∣<= d
.
Constraints:
1<= arr1.length
,arr2.length
<=500 −1000<= arr1[i]
,arr2[j]
<=1000 0<= d
<=100
Solution
The essence of this solution is to find the distance value between two arrays using the sort and search pattern. The approach begins by sorting arr2
, and then for each element in arr1
, binary search is used to find the closest element in arr2
. Binary search is used on arr2
 because it lets us quickly find the closest elements without checking all values, ensuring the solution is faster and avoiding brute-force comparisons for each element. The condition we check is whether the absolute difference between an element in arr1
 and its closest element(s) in arr2
 is greater than the threshold d
. This is sufficient because elements further from the closest values will always have a greater absolute difference and, therefore, cannot violate the condition. The binary search helps us identify these closest high and low values. If no such element in arr2
 violates the condition for a given element in arr1
, we increment the distance value by 1.
Now, let’s look at the steps of the solution:
We start by sortingÂ
arr2
 to use binary search on it.We initialize a variableÂ
distance
 to0 , which will keep track of how many elements fromÂarr1
 meet the criteria of having no corresponding element inÂarr2
 with an absolute difference ofÂ<= d
.We iterate through
arr1
and for each element,arr1[i]
, we perform the following:InitializeÂ
left
 andÂright
 pointers to represent the binary search boundaries inÂarr2
.Set a boolean flagÂ
valid
 to TRUE, which indicates whether the current element,arr1[i]
, inÂarr1
, satisfies the distance condition. TheÂvalid
 flag is initially set to TRUE because, by default, we assume that the current elementÂarr1[i]
 satisfies the distance value condition. The flag will only be set toÂFalse
 if a violating element is found inÂarr2
.We then perform a binary search onÂ
arr2
 to find the closest element to the current element,Âarr[i]
. This is done by checking the middle element,arr2[mid]
, of the current search window and adjusting the search boundaries (left
 andÂright
) based on whetherÂarr2[mid]
 is less than or greater thanÂarr[i]
.After the binary search, we check:
If the closest element inÂ
arr2
 (on the right side) is within the distanceÂd
 fromÂarr[i]
, we setÂvalid
 to FALSE.Similarly, we check the closest element on the left side to see if it violates the condition; we setÂ
valid
 to FALSE.If no element fromÂ
arr2
 violates the condition for the currentÂarr[i]
, we increment theÂdistance
by1 . This indicates thatÂarr[i]
 satisfies the distance value condition and contributes to the distance value.
Finally, we return the value ofÂ
distance
, which gives the number of elements fromÂarr1
 that satisfy the distance value condition.
Let’s look at the following illustration to get a better understanding of the solution: