[LeetCode] Find the Distance Value Between Two Arrays

1385. 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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int findTheDistanceValue(vector<int>& A, vector<int>& B, int d) {
int n = A.size(), m = B.size();
sort(begin(B), end(B));
int res = 0;
for(auto& a : A) {
int lo = a - d;
auto it = lower_bound(begin(B), end(B), lo);
if(it == end(B) or *it > a + d) res++;
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2022/06/03/PS/LeetCode/find-the-distance-value-between-two-arrays/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.