Open In App

JavaScript Program to Find the Distance Value between Two Arrays

Last Updated : 23 Apr, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

We will be given two integer arrays and an integer d. The task is to calculate the distance between the two given arrays based on the given integer value. The distance value will be calculated as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.

Example:

Input: arr1 = [4, 5, 8], arr2 = [10, 9, 1, 8], d = 2
Output: 2
Explanation: 
For arr1[0]=4 we have:
|4-10|=6 is greater than d(2)
|4-9|=5 is greater than d(2)
|4-1|=3 is greater than d(2)
|4-8|=4 is greater than d(2),
This element 4, of arr1 will be considered, as none of the distance
value is less than or equal to given distance value. Hence, count will be one here.

For arr1[1]=5 we have:
|5-10|=5 is greater than d(2)
|5-9|=4 is greater than d(2)
|5-1|=4 is greater than d(2)
|5-8|=3 is greater than d(2)
This element 5, of arr1 will be considered, as none of the distance
value is less than or equal to given distance value. Hence, count will be two here.

For arr1[2]=8 we have:
|8-10|=2 is less than equal to d(2)
|8-9|=1 is less than equal to d(2)
|8-1|=7 is greater than d(2)
|8-8|=0 is less than equal to d(2)
This element will not be considered, as some of the calculated values
satisfies the condition. Hence, count will be returned here with value as 2.

These are the approaches to find the distance value between two arrays:

In this approach, we use sorting and binary search to solve the above problems. First we sort the second array and then use binary search to find the elements in the second array that meet the distance requirement for each element in the first array.

Example: The below code implements the JavaScript sort() method with binary search to find distance between two arrays.

JavaScript
function findTheDistanceValue(arr1, arr2, d) {
    function check(a) {
        let i = arr2.findIndex
            (num => num > a - d);
        return i === -1 || 
            arr2[i] > a + d;
    }

    arr2.sort((a, b) => a - b);
    return arr1.reduce(
        (count, a) => 
            count + (check(a) ? 1 : 0), 0);
}

console.log(
    "Distance: ", findTheDistanceValue(
        [4, 5, 8], [10, 9, 1, 8], 2));

console.log(
    "Distance: ", findTheDistanceValue(
        [1, 4, 2, 3], 
        [-4, -3, 6, 10, 20, 30], 1)); 

Output
Distance:  2
Distance:  4

Time Complexity: O((m)logm), where m is the length of arr2
Auxiliary Space: O(1)

Using two nested loops

In this approach, we iterate through each element of first array and check if there is any element in arr2 that satisfies the condition and If such element is found, we increment the count variable. Finally, we return the count as the output.

Syntax:

 for(let i = 0; i<firstArrayLength; i++){
// First loop logic
for(let i=0; i<secondArrayLength; i++){
// Second loop logic
}
}

Example: The below code implements the nested JavaScript for loops to find distance between two arrays.

JavaScript
function findTheDistanceValue(arr1, arr2, d) {
    let count = 0;
    for (let i = 0; i < arr1.length; i++) {
        let valid = true;

        for (let j = 0; j < arr2.length; j++) {
            if (Math.abs(arr1[i] - arr2[j]) <= d) {
                valid = false;
                break;
            }
        }
        if (valid) {
            count++;
        }
    }
    return count;
}

console.log(
    "Distance:",
    findTheDistanceValue([1, 4, 2, 3],
        [-4, -3, 6, 10, 20, 30], 1));
console.log(
    "Distance:",
    findTheDistanceValue([4, 5, 8],
        [10, 9, 1, 8], 2));

Output
Distance: 4
Distance: 2

Time Complexity: O(n*m), where n is the length of arr1 and m is the length of arr2
Auxiliary Space: O(1)



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads