Find the Distance Value Between Two Arrays

🏠 ⬅️ ➡️

Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.

The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.

Example 1:

Input: arr1 = [4,5,8], arr2 = [10,9,1,8], d = 2 Output: 2 Explanation: For arr1[0]=4 we have: |4-10|=6 > d=2 |4-9|=5 > d=2 |4-1|=3 > d=2 |4-8|=4 > d=2 For arr1[1]=5 we have: |5-10|=5 > d=2 |5-9|=4 > d=2 |5-1|=4 > d=2 |5-8|=3 > d=2 For arr1[2]=8 we have: |8-10|=2 <= d=2 |8-9|=1 <= d=2 |8-1|=7 > d=2 |8-8|=0 <= d=2

Example 2:

Input: arr1 = [1,4,2,3], arr2 = [-4,-3,6,10,20,30], d = 3 Output: 2

Example 3:

Input: arr1 = [2,1,100,3], arr2 = [-5,-2,10,-3,7], d = 6 Output: 1

Constraints:

  • 1 <= arr1.length, arr2.length <= 500
  • -1000 <= arr1[i], arr2[j] <= 1000
  • 0 <= d <= 100

Note: This problem is from LeetCode.
Compiled
Executed
Correct
program main
    implicit none

    integer, parameter :: n = 3
    integer, parameter :: m = 5
    integer, parameter :: d = 2

    integer, dimension(n) :: arr1 = (/ 4, 5, 8 /)
    integer, dimension(m) :: arr2 = (/ 10, 9, 1, 8, 20 /)

    write (*, '(I0)') distance(arr1, arr2, d)

contains

    function distance(arr1, arr2, d) result(distance_value)
        implicit none
        integer, dimension(:), intent(in) :: arr1
        integer, dimension(:), intent(in) :: arr2
        integer, intent(in) :: d
        integer :: distance_value
        integer :: i, j

        distance_value = 0
        do i = 1, size(arr1)
            do j = 1, size(arr2)
                if (abs(arr1(i) - arr2(j)) <= d) then
                    exit
                end if
            end do
            if (j == size(arr2)+1) then
                distance_value = distance_value + 1
            end if
        end do
    end function distance

end program main
Compiled
Executed
Correct
module distance
  implicit none
contains
  function distance(arr1, arr2, d) result(distance_value)
    integer, intent(in) :: arr1(:), arr2(:), d
    integer :: distance_value

    ! Local variables
    integer :: i, j, diff

    ! Initialize the distance value to the length of arr1
    distance_value = size(arr1)

    ! Loop through each element of arr1
    do i = 1, size(arr1)
      ! Loop through each element of arr2
      do j = 1, size(arr2)
        ! Calculate the difference between the two elements
        diff = abs(arr1(i) - arr2(j))

        ! If the difference is less than or equal to d,
        ! then the distance value should be decremented
        if (diff <= d) then
          distance_value = distance_value - 1
          exit
        end if
      end do
    end do
  end function distance
end module distance

program test_distance
  use distance
  implicit none

  ! Test case 1
  print *, distance([4,5,8], [10,9,1,8], 2)

  ! Test case 2
  print *, distance([1,4,2,3], [-4,-3,6,10,20,30], 3)

  ! Test case 3
  print *, distance([2,1,100,3], [-5,-2,10,-3,7], 6)
end program test_distance
🌐 Data from online sources
def findTheDistanceValue(arr1, arr2, d):
    distance = 0
    for a in arr1:
        found = True
        for b in arr2:
            if abs(a - b) <= d:
                found = False
                break
        if found:
            distance += 1
    return distance

For each element a in array arr1, we iterate through all elements b in array arr2 and check the condition |a-b| <= d. If we find an element b that satisfies the condition, we stop searching and move to the next element of arr1. If we haven't found any such element, we increment the distance value. Finally, we return the distance value after processing all elements of arr1.

🌐 Data from online sources
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
    int distance = 0;
    for (int a : arr1) {
        bool found = true;
        for (int b : arr2) {
            if (abs(a - b) <= d) {
                found = false;
                break;
            }
        }
        if (found) distance++;
    }
    return distance;
}

For each element a in array arr1, we iterate through all elements b in array arr2 and check the condition |a-b| <= d. If we find an element b that satisfies the condition, we stop searching and move to the next element of arr1. If we haven't found any such element, we increment the distance value. Finally, we return the distance value after processing all elements of arr1.