
Let the random number be ‘X’
| X- ARR[i] | > ‘D’ for ‘i’ in range 0 to length of ‘ARR’-1.
For the given ‘ARR’ = [ 13,1,4 ] and B=[7,6,9] and ‘D’=3, the answer will be 1 as only 9 satisfies the given condition among the given numbers.
The first line of the input contains an integer, 'T,’ denoting the number of test cases.
The first line of each test case contains three integers, 'N’ denoting the number of numbers in array ‘ARR’, ‘M’ representing the number of elements in array ‘B’, and integer ‘D’.
The second line contains ‘N’ numbers denoting the numbers in the array ‘ARR’.
The third line contains ‘M’ numbers denoting the numbers in array ‘B’.
For each test case, print a single integer denoting the count of numbers among array ‘B’ satisfying the given condition.
Print the output of each test case in a separate line.
You do not need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 5000.
1 <= M <= 5000.
1 <= D <= 1000.
1 <=ARR[i] <= 10^6
1 <=B[i] <=10^6.
Time limit: 1 sec
In this approach, we will check the absolute difference of each element of ‘B’ with all elements of ‘ARR’. If All the absolute difference’s value for B[i] is greater than ‘D’ we found a number that fulfills the given condition. So we increment the answer count by 1.
If we observe the given equation, we can conclude that any number ‘X’ doesn’t satisfy the equation if there is any number in ‘ARR’ in range ‘X’-’D’ to ‘X’+’D’(both inclusive). Otherwise, the number ‘X’ satisfies the condition.
In this approach, we will first sort the ‘ARR’ array so that we can apply binary search over it.
For each number in ‘B’, we will find the number just greater than B[i] and just smaller than B[i] in array ‘ARR’.And if any of these two numbers lies in this range (B[i] -D, B[i]+D),B[i] fails the condition.Otherwise, we will increment ‘ANS’.