JavaScript Program to Find the Distance Value between Two Arrays
Last Updated :
23 Apr, 2024
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:
Using sorting and binary search
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));
OutputDistance: 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));
OutputDistance: 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)
Similar Reads
JavaScript Program to Find Shortest Distance Between Two Words in an Array of Words Given an array of words and two target words, the task is to find the shortest distance (minimum number of words) between the two target words in the array. The distance is calculated by counting the number of words between the two target words, excluding the target words themselves. Approaches to f
2 min read
JavaScript Program to Find Common Elements Between Two Sorted Arrays using Binary Search Given two sorted arrays, our task is to find common elements in two sorted arrays then the program should return an array that contains all the elements that are common to the two arrays. The arrays can contain duplicate elements, that is, values at one index in an array can be equal to the values a
3 min read
JavaScript Program to find Maximum Distance Between two Occurrences of Same Element in Array Given an array of repeated elements, the task is to find the maximum distance between two occurrences of an element.Example:Input : arr[] = {3, 2, 1, 2, 1, 4, 5, 8, 6, 7, 4, 2}Output: 10// maximum distance for 2 is 11-1 = 10 // maximum distance for 1 is 4-2 = 2 // maximum distance for 4 is 10-5 = 5
4 min read
How to find the distance between two points? Answer: The distance between points (x1 , y1) and (x2 , y2) is â(x2 - x1)2 + (y2 - y1)2The length of the wire used to cover the length between two points is the distance between two points. In case the two points are situated on the same horizontal or same vertical line, the distance between the two
3 min read
Javascript Program to Find a pair with the given difference Given an unsorted array and a number n, find if there exists a pair of elements in the array whose difference is n. Examples: Input: arr[] = {5, 20, 3, 2, 50, 80}, n = 78Output: Pair Found: (2, 80)Input: arr[] = {90, 70, 20, 80, 50}, n = 45Output: No Such PairNative Approach:The simplest method is t
4 min read
Javascript Program to Find closest number in array Given an array of sorted integers. We need to find the closest value to the given number. Array may contain duplicate values and negative numbers. Examples: Input : arr[] = {1, 2, 4, 5, 6, 6, 8, 9} Target number = 11 Output : 9 9 is closest to 11 in given array Input :arr[] = {2, 5, 6, 7, 8, 8, 9};
4 min read
Java Program to Compare two Boolean Arrays Two arrays are equal if they contain the same elements in the same order. In java, we can compare two Boolean Arrays in 2 ways: By using Java built-in method that is .equals() method.By using the Naive approach. Examples: Input : A = [true , true , false] A1 = [true, true, false] Output: Both the ar
3 min read
How to Calculate the Levenshtein Distance Between Two Strings in Java Using Recursion? In Java, the Levenshtein Distance Algorithm is a pre-defined method used to measure the similarity between two strings and it can be used to calculate the minimum number of single-character edits (inserts, deletions, or substitutions) required to change one string into another. Prerequisites: Recurs
4 min read
Minimum distance between the given two words Given a list of words followed by two words, the task is to find the minimum distance between the given two words in the list of words.Examples:Input: S = { "the", "quick", "brown", "fox", "quick"}, word1 = "the", word2 = "fox"Output: 3Explanation: Minimum distance between the words "the" and "fox"
4 min read
Find the Minimum Distance Between Two Numbers in PHP Given two numbers A and B, and an array arr containing multiple occurrences of these numbers, the task is to find the minimum distance between the given numbers X and Y. It is guaranteed that the array will contain at least one occurrence of both A and B. Examples: Input:A[] = {1,2,3,2}x = 1, y = 2O
4 min read