Loading problem…
Given a non-negative integer x, return the square root of x rounded down to the nearest integer. The returned integer should be non-negative as well. You must not use any built-in exponent function or operator.
Example 1
Input: x = 8
Output: 2
Explanation: √8 ≈ 2.83. Rounding down gives 2, since 2 × 2 = 4 ≤ 8 but 3 × 3 = 9 > 8.
Example 2
Input: x = 4
Output: 2
Explanation: 4 is a perfect square: 2 × 2 = 4.
Example 3
Input: x = 0
Output: 0
Explanation: The square root of 0 is 0.
The floor of the square root lies in [1, x/2]. Binary search that range: for a middle guess, compare its square to x and discard half the candidates each step.
Run checks all cases above. Submit evaluates all test cases.