Skip to content

Commit

Permalink
Time: 4 ms (83.52%), Space: 44.8 MB (34.59%) - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
saikatkar committed May 29, 2022
1 parent 1f94ab5 commit b63f9c4
Showing 1 changed file with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Solution {
public int findTheDistanceValue(int[] arr1, int[] arr2, int d) {
int count = 0;
Arrays.sort(arr2);
for (int a : arr1) {
if (search(arr2, a-d, a+d)) {
count++;
}
}
return arr1.length-count;
}

private boolean search (int [] arr, int r1, int r2) {
int start = 0;
int end = arr.length-1;
while (start <= end) {
int mid = start + (end-start)/2;
if (arr[mid] >= r1 && arr[mid] <= r2) {
return true;
}
if (arr[mid] < r1) {
start = mid+1;
} else if (arr[mid] > r2) {
end = mid-1;
}
}
return false;
}
}

0 comments on commit b63f9c4

Please sign in to comment.