Description
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.
Examples
Input:
x = 4Output:
2Explanation:
Since 2 * 2 = 4 exactly, the integer square root is 2 with no rounding needed.
Input:
x = 8Output:
2Explanation:
The square root of 8 is 2.82842..., rounded down to 2.
Input:
x = 1Output:
1Explanation:
Since 1 * 1 = 1 exactly, the integer square root is 1. This is a base case where the number equals its own square root.
Input:
x = 0Output:
0Explanation:
Zero is a special case: 0 * 0 = 0, so the integer square root of 0 is trivially 0.
Constraints
- •
0 ≤ x ≤ 2³¹ - 1