Solution: Find the Distance Value Between Two Arrays
Let’s solve the Find the Distance Value Between Two Arrays problem using the Sort and Search pattern.
We'll cover the following
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 inarr2
such thatd
.
Constraints:
arr1.length
,arr2.length
arr1[i]
,arr2[j]
d
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
to, which will keep track of how many elements from arr1
meet the criteria of having no corresponding element inarr2
with an absolute difference of<= d
.We iterate through
arr1
and for each element,arr1[i]
, we perform the following:Initialize
left
andright
pointers to represent the binary search boundaries inarr2
.Set a boolean flag
valid
to TRUE, which indicates whether the current element,arr1[i]
, inarr1
, satisfies the distance condition. Thevalid
flag is initially set to TRUE because, by default, we assume that the current elementarr1[i]
satisfies the distance value condition. The flag will only be set toFalse
if a violating element is found inarr2
.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
andright
) based on whetherarr2[mid]
is less than or greater thanarr[i]
.After the binary search, we check:
If the closest element in
arr2
(on the right side) is within the distanced
fromarr[i]
, we setvalid
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 currentarr[i]
, we increment thedistance
by. 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 fromarr1
that satisfy the distance value condition.
Let’s look at the following illustration to get a better understanding of the solution:
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.