Skip to content

Sqrt(x)

EasyBinary Search
Decode it first. Clues, constraints, pattern and plan before any code.

Given a non-negative integer x, return its square root rounded down to the nearest whole number. You may not use a built-in power or square-root function (no math.sqrt, **, Math.sqrt or Math.pow).

x = 4   →  2
x = 8   →  2   (the real root is 2.828..., rounded down)
x = 0   →  0

Constraints

  • 0 <= x <= 2^31 - 1