Find the Distance Value Between Two Arrays
Too many down votes. Read the problem statement in the Solutions tab.
The task is to return the distance value between two arrays arr1
and arr2
. The distance value is the count of elements in arr1
for which there is no element in arr2
such that the absolute difference between them is less than or equal to d
.
Let’s follow a simple brute force approach where for each element in arr1
, we check all elements in arr2
and increase our count if no such element is found in arr2
which satisfies the given condition.
Here is the Python code:
|
|
In the above code:
- We initialize a variable
count
to 0. - For each element (
num1
) inarr1
, we check all elements (num2
) inarr2
to see if the absolute difference betweennum1
andnum2
is more thand
. - If it is for all
num2
, we incrementcount
because there is no element inarr2
satisfying the condition for thatnum1
. - Finally, we return
count
which is our answer.
The time complexity of this solution is O(n*m), where n is the size of arr1
and m is the size of arr2
.