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
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
2
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
temp.f95:4:19: 4 | function distance(arr1, arr2, d) result(distance_value) | 1 Error: MODULE attribute of ‘distance’ conflicts with PROCEDURE attribute at (1) temp.f95:5:46: 5 | integer, intent(in) :: arr1(:), arr2(:), d | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:6:29: 6 | integer :: distance_value | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:9:25: 9 | integer :: i, j, diff | 1 Error: Unexpected data declaration statement in CONTAINS section at (1) temp.f95:12:31: 12 | distance_value = size(arr1) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:15:24: 15 | do i = 1, size(arr1) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:17:26: 17 | do j = 1, size(arr2) | 1 Error: Unexpected DO statement in CONTAINS section at (1) temp.f95:19:37: 19 | diff = abs(arr1(i) - arr2(j)) | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:23:27: 23 | if (diff <= d) then | 1 Error: Unexpected block IF statement in CONTAINS section at (1) temp.f95:24:45: 24 | distance_value = distance_value - 1 | 1 Error: Unexpected assignment statement in CONTAINS section at (1) temp.f95:25:14: 25 | exit | 1 Error: EXIT statement at (1) is not within a construct temp.f95:26:11: 26 | end if | 1 Error: Expecting END MODULE statement at (1) temp.f95:27:9: 27 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:28:7: 28 | end do | 1 Error: Expecting END MODULE statement at (1) temp.f95:29:5: 29 | end function distance | 1 Error: Expecting END MODULE statement at (1) temp.f95:33:7: 33 | use distance | 1 Fatal Error: Cannot open module file ‘distance.mod’ for reading at (1): No such file or directory compilation terminated.
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
.
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
.