Sqrt(x)

Easy

Given a non-negative integer x, return the square root of x rounded down to the nearest integer.

You may not use any built-in exponent or square-root function.

Example 1

mySqrt()
Input
x = 4
Output
2

Example 2

mySqrt()
Input
x = 8
Output
2

Why

sqrt(8) = 2.828…, truncated to 2.
Constraints
0 <= x <= 2^31 - 1

Topics

MathBinary Search

Implement mySqrt in the editor and return the answer — printing it is never graded.