Dodomeki
Sign in

56. Integer root without shortcuts

Easy

Given a non-negative integer x, find its integer square root: the largest integer r such that r * r <= x. Do not use square-root functions.

Example 1
Input: x = 0
Output: 0
Example 2
Input: x = 8
Output: 2
Explanation: Since 2² ≤ 8 and 3² > 8, the answer is 2.
Example 3
Input: x = 16
Output: 4
Constraints
  • 0 <= x <= 2^31 - 1
Binary searchMath
Caso 1
x =
0
Expected =
0
Caso 2
x =
8
Expected =
2
Caso 3
x =
16
Expected =
4

Run checks these 3 cases. Submit runs all 11, including hidden ones.