-
Notifications
You must be signed in to change notification settings - Fork 0
/
5. Find the Distance Value Between Two Arrays.cpp
56 lines (47 loc) · 1.32 KB
/
5. Find the Distance Value Between Two Arrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <vector>
using namespace std;
// GENERIC TEMPLATE
bool condition(int midVal, int target) {
// Implement the condition based on the problem requirements
return midVal < target;
}
bool checkIfThisIsDistanceValue(int index, vector<int>& arr, int d, int val){ // 1, 8, 9, 10
if(index == 0){
if(arr[index]-val <= d)
return false;
return true;
}
if(index == arr.size()){
if(val-arr[index-1] <= d)
return false;
return true;
}
if(arr[index]-val <= d || val-arr[index-1] <= d){
return false;
}
return true;
}
int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
int count = 0;
sort(arr2.begin(), arr2.end());
for (int i = 0; i < arr1.size(); i++) {
int target = arr1[i];
int left = 0;
int right = arr2.size() - 1;
int ans = -1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (condition(arr2[mid], target)) {
ans = mid;
left = mid + 1;
} else {
right = mid - 1;
}
}
if( checkIfThisIsDistanceValue(ans+1, arr2, d, arr1[i]) ){
count++;
}
}
return count;
}