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.

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]<=|arr1[i]-arr2[j]| <= d.

Constraints:

  • 1<=1 <= arr1.length, arr2.length <=500 <= 500

  • 1000<=-1000 <= arr1[i], arr2[j] <=1000<= 1000

  • 0<=0 <= d <=100<= 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:

  1. We start by sorting arr2 to use binary search on it.

  2. We initialize a variable distance to 00, 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.

  3. We iterate through arr1 and for each element, arr1[i], we perform the following:

    1. Initialize left and right pointers to represent the binary search boundaries in arr2.

    2. 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.

    3. 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].

    4. After the binary search, we check:

      1. If the closest element in arr2 (on the right side) is within the distance d from arr[i], we set valid to FALSE.

      2. Similarly, we check the closest element on the left side to see if it violates the condition; we set valid to FALSE.

      3. If no element from arr2 violates the condition for the current arr[i], we increment the distance by 11. This indicates that arr[i] satisfies the distance value condition and contributes to the distance value.

  4. 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:

Level up your interview prep. Join Educative to access 70+ hands-on prep courses.