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 = 4 Output: 2 Explanation: The square root of 4 is 2, so we return 2.
Example 2:
Input: x = 8 Output: 2 Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer, 2 is returned.
Constraints:
0 <= x <= 231 - 1When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
To find the square root of a number using the brute force approach, you systematically test every whole number starting from zero and going upwards. You stop and have your answer when multiplying a number by itself either matches your target number or just barely exceeds it.
Here's how the algorithm would work step-by-step:
def my_sqrt(target_number):
# Handle zero explicitly since zero multiplied by itself is always zero
if target_number == 0:
return 0
current_candidate = 0
# Test consecutive numbers until the square exceeds the target value
while current_candidate * current_candidate <= target_number:
current_candidate += 1
# Return the previous integer because the current one exceeded the target
return current_candidate - 1Instead of testing every single number to find the square root, we can use a guessing game approach that narrows down the possibilities very quickly. By repeatedly splitting our range of possible answers in half, we can zero in on the correct whole number in just a few steps.
Here's how the algorithm would work step-by-step:
class Solution:
def mySqrt(self, target_number: int) -> int:
if target_number < 2:
return target_number
lower_bound = 0
upper_bound = target_number
stored_result = 0
# Halving the search range ensures logarithmic time complexity.
while lower_bound <= upper_bound:
middle_number = (lower_bound + upper_bound) // 2
if middle_number * middle_number == target_number:
return middle_number
# Discarding halves prevents redundant checks and optimizes performance.
elif middle_number * middle_number < target_number:
stored_result = middle_number
lower_bound = middle_number + 1
else:
upper_bound = middle_number - 1
# Returning the floored value guarantees the integer square root constraint.
return stored_result| Case | How to Handle |
|---|---|
| Input x is 0 | Binary search or iterative bounds must include 0 to return 0 immediately without division by zero errors. |
| Input x is 1 | The algorithm must correctly evaluate that the square root of 1 is 1 rather than 0. |
| Maximum 32-bit signed integer values near 2^31 - 1 | Midpoint calculations using (left + right) / 2 can overflow integers, so we use left + (right - left) / 2. |
| Squaring the midpoint during binary search like mid * mid | Multiplying two 32-bit integers can exceed integer limits, requiring the use of 64-bit long data types for the product. |
| Non-perfect square numbers like 8 or 10 | The binary search naturally converges and truncates downwards by keeping the last valid lower bound where mid * mid <= x. |
| Negative integer inputs | The problem description specifies non-negative integers, but robust code should handle this by throwing an exception or returning -1. |