69. Sqrt(x)
202409012122
tags: #binary-search
var mySqrt = function(x) {
let start = 1;
let end = Math.floor(x / 2) + 1;
while (start <= end) {
const mid = Math.floor((end - start) / 2) + start;
const product = mid * mid;
if (product === x) {
return mid;
}
if (product < x) {
start = mid + 1;
} else {
end = mid - 1;
}
}
return end;
};