514. Find the Distance Value Between Two Arrays

MediumBinary SearchBinary SearchArray

Given two arrays a and b and an integer d, the distance value is the number of elements in a for which there is no element in b within distance d (that is, no b[j] with |a[i] - b[j]| <= d). Return the distance value. Sort b and use binary search. The input is JSON {a, b, d}. Return the count.

Input: JSON {a, b, d}.

Output: Integer — the distance value.

Examples

Example 1
Input: {"a":[4,5,8],"b":[10,9,1,8],"d":2}
Output: 2
Explanation: 4 and 5 have no near element in b.
Example 2
Input: {"a":[1,4,2,3],"b":[-4,-3,6,10,20,30],"d":3}
Output: 2
Explanation: Two elements isolated.
Example 3
Input: {"a":[1],"b":[1],"d":0}
Output: 0
Explanation: 1 is within distance 0 of 1.

Constraints

Asked by

BloombergMicrosoftGoogle
Solve this problem in the editor →