call(back)
Algorithms & Data StructuresmediumSign in to star problems

Largest Sensor Distance

For any integer in array1, the closest integer in array2 is its sensor, and the absolute difference between the two is its sensor distance. Return the largest sensor distance.

array1 = [1, 5, 11], array2 = [4, 12]
distances: 1->4 is 3, 5->4 is 1, 11->12 is 1   ->  3

Neither array is sorted, and positions may be negative. Return 0 when either array is empty.

Hints

Hint 1

Sort the sensors once. For each target, its closest sensor is one of the two neighbors of its insertion point — bisect gives you both in O(log m).

Hint 2

If both sides are sorted, you never need binary search: sweep a single sensor pointer forward while the next sensor is at least as close — it never moves backward across targets.

Asked at