# JavaScript O((m + n) log n) using binary search

• ``````var findRadius = function(houses, heaters) {
heaters.sort((a, b) => a - b);
return houses.reduce((max, house) => Math.max(max, closestHeaterDistance(house, heaters, 0, heaters.length - 1)), 0);
};

function closestHeaterDistance(house, heaters, low, high) {
if (low === high) return Math.abs(house - heaters[low]);
let mid = Math.floor((low + high) / 2);
if (house < heaters[mid]) {
if (mid === 0) return Math.abs(house - heaters[0]);
if (house > heaters[mid - 1]) {
return Math.min(Math.abs(house - heaters[mid]), Math.abs(house - heaters[mid - 1]));
}
return closestHeaterDistance(house, heaters, low, mid - 1);
} else if (house > heaters[mid]) {
if (mid === heaters.length - 1) return Math.abs(house - heaters[heaters.length - 1]);
if (house < heaters[mid + 1]) {
return Math.min(Math.abs(house - heaters[mid]), Math.abs(house - heaters[mid + 1]));
}
return closestHeaterDistance(house, heaters, mid + 1, high)
}
return 0;
}
``````

The strategy is to maximize the minimum distance, finding the minimum by binary search.

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.