From b63f9c4a1561ef7e4f8acc4ab33150c830f31b55 Mon Sep 17 00:00:00 2001 From: Saikat Kar Date: Sun, 29 May 2022 23:02:11 +0530 Subject: [PATCH] Time: 4 ms (83.52%), Space: 44.8 MB (34.59%) - LeetHub --- ...the-distance-value-between-two-arrays.java | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 1385-find-the-distance-value-between-two-arrays/1385-find-the-distance-value-between-two-arrays.java diff --git a/1385-find-the-distance-value-between-two-arrays/1385-find-the-distance-value-between-two-arrays.java b/1385-find-the-distance-value-between-two-arrays/1385-find-the-distance-value-between-two-arrays.java new file mode 100644 index 0000000..3249dbe --- /dev/null +++ b/1385-find-the-distance-value-between-two-arrays/1385-find-the-distance-value-between-two-arrays.java @@ -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; + } +} \ No newline at end of file