var findRadius = function(houses, heaters) { heaters = [...heaters.sort((a, b) => a - b)] let res = 0 for (const home of houses) { let left = 0 let right = heaters.length - 1 let mid; let ans = Math.abs(home - heaters[0]); if(home < heaters[left]){ ans = heaters[left] - home; }elseif (home>heaters[right]){ ans = home-heaters[right] }else{ while (left < right) { mid = left + ((right - left) >> 1) if (right - left === 1) { ans = Math.min(home - heaters[left], heaters[right] - home); break; }elseif (heaters[left+1]>home) { ans = Math.min(home - heaters[left], heaters[left+1] - home); break; }elseif(heaters[right-1]<home){ ans = Math.min(home - heaters[right-1], heaters[right] - home); break; } if (heaters[mid] < home) { left = mid; } elseif(heaters[mid] > home) { right = mid; } else { ans = 0; break; } } } res = Math.max(res, ans); } return res; };