Given a non-negative integer x, return the integer square root of x — the largest integer r such that r * r <= x. The fractional part is truncated (rounded down), and you must not use any built-in square-root function.
Input / output
x: integerfloor(sqrt(x))Examples
x = 4 returns 2.x = 8 returns 2 (sqrt(8) = 2.828..., truncated to 2).x = 0 returns 0.Constraints
0 <= x <= 2,147,483,647Follow-up
Binary search gives O(log x). Guard the midpoint test against overflow (in fixed-width languages) by comparing mid to x / mid instead of computing mid * mid. Could Newton's method converge faster?