You are 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.
pow(x, 0.5) in c++ or x ** 0.5 in python.Example 1:
Input: x = 9
Output: 3Example 2:
Input: x = 13
Output: 3Constraints:
0 <= x <= ((2^31)-1)The square root of a number x is the largest integer i such that i * i <= x. We can find this by simply checking each integer starting from 1 until squaring it exceeds x. The answer is the last integer whose square did not exceed x.
x is 0 by returning 0.res to 1 to track the potential answer.1 to x. For each integer i, check if i * i > x.i * i > x, return the previous valid result res.res = i and continue.res after the loop.Most programming languages provide a built-in square root function that computes the result efficiently using optimized mathematical algorithms (often Newton's method or similar). We can leverage this and simply truncate the result to get the integer floor of the square root.
x.Since we are looking for the largest integer whose square is at most x, and the squares of integers are monotonically increasing, we can use binary search. The search space is [0, x], and we narrow it down by checking if the middle value squared is less than, greater than, or equal to x.
l = 0, r = x, and res = 0 to store the answer.l <= r:m = l + (r - l) / 2.m * m > x, the answer must be smaller, so set r = m - 1.m * m < x, m is a valid candidate. Store it in res and search for a larger value by setting l = m + 1.m * m == x, we found the exact square root, so return m.res after the loop.We can exploit a mathematical property: sqrt(x) = 2 * sqrt(x / 4). By right-shifting x by 2 bits (dividing by 4), we recursively compute the square root of a smaller number. Then we left-shift the result by 1 (multiply by 2) to scale it back up. Finally, we check if incrementing by 1 still gives a valid square root.
x < 2, return x (since sqrt(0) = 0 and sqrt(1) = 1).l = mySqrt(x >> 2) << 1. This gets an approximate lower bound.r = l + 1 as the candidate one larger.r * r > x, return l. Otherwise, return r.Newton's method is a numerical technique for finding roots of equations. To find sqrt(x), we solve r^2 = x, or equivalently find the root of f(r) = r^2 - x. Newton's iteration formula gives us r_new = (r + x/r) / 2. Starting with an initial guess of x, we repeatedly apply this formula until r^2 <= x, which converges quickly to the answer.
r = x as the starting guess.r * r > x, update r = (r + x / r) / 2 (using integer division, or right shift by 1).r^2 <= x.r as the integer square root.When computing m * m during binary search, the result can overflow if m is large (e.g., close to 46340 for 32-bit integers). Always cast to a 64-bit type before multiplication, such as (long)m * m in Java or (long long)m * m in C++, to prevent incorrect comparisons.
A subtle mistake is returning m when m * m < x instead of tracking it as a candidate and continuing to search for a larger valid value. The correct approach is to store m in a result variable when m * m <= x and keep searching, returning the stored result when the loop ends.