Sqrtx
                                
                                
                                    
                                
                                
                                    
                                        To solve this coding challenge, we need to implement a function that computes the square root of a given non-negative integer \(x\), rounded down to the nearest integer. The challenge specifies that we must not use any built-in exponent function or operator.
                                    
                                
                                
                            
                                
                                    
                                
                                
                                    
                                        
                                
                        
                    
                    
                
                    
                Explanation
We can approach this problem using the binary search algorithm. Binary search is effective here because it allows us to narrow down the potential square root quickly by halving the search space in each step. The essential idea is to find the largest integer \(m\) such that: \[ m^2 \leq x \] We will use a loop to repeatedly halve our search space based on the value of \( m^2 \) compared to \( x \). Here's the detailed step-by-step explanation of the algorithm:- Initial Check: If \( x = 0 \), then the square root is 0.
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Binary Search Setup:
                                    
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                         We initialize two pointers, 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        andleft, to represent the search space.rightstarts at 1, andleftstarts at \( x \).right
- Binary Search Loop:
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Calculate 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        as the average ofmidandleft.right
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Check if 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        equals \( x \).mid * mid
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        is equal to \( x \), returnmid * midsince we found the exact square root.mid
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If `mid 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                         mid` is less than \( x \) but \( (mid + 1) 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                         (mid + 1) \) is greater than \( x \), return 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        as it means \( mid \) is the largest integer where \( mid^2 \leq x \).mid
- Adjust the search range based on the comparison:
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        is greater than \( x \), movemid * midtoright.mid - 1
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        is less than \( x \), movemid * midtoleft.mid + 1
- Return Result: The loop continues until we narrow down the potential values, and we return the appropriate integer value. Below is the pseudocode implementing this logic:
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Function Entry:
                                    
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                         We call 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        , where \(x\) is the integer for which we want to find the square root.mySqrt(x)
- If \(x\) is 0, return 0 immediately, because the square root of 0 is 0.
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Initialize 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        to 1 andlefttoright.x
- Binary Search Loop:
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Calculate 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        as the integer division ofmid.(left + right) // 2
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Check if `mid 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                         mid
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        xis less than or equal to(mid + 1) (mid + 1)` is greater than `x`:, but
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If true, return 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        becausemidis the largest integer whose square is less than or equal tomid.x
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        is greater thanmid * mid, adjust the search range by settingxtoright.mid - 1
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        If 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        is less thanmid * mid, adjust the search range by settingxtoleft.mid + 1
- 
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                        Conclusion:
                                    
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                            
                                
                                
                                    
                                        
                                            
                                                
                                
                                
                                    
                                         The loop iteratively narrows down the search space until we find the largest integer where its square is less than or equal to 
                                    
                                
                                
                            
                                            
                                                
                                
                                
                                    
                                        
                                        
                                        
                                        
                                        
                                        . Return this integer.x
                                            
# Function to find the square root of x, rounded down to the nearest integer
function mySqrt(x):
# Handle the edge case where x is 0
if x == 0:
return 0
# Initialize the binary search range
left = 1
right = x
# Perform binary search
while left <= right:
# Calculate the middle point
mid = (left + right) // 2
# Check if mid^2 is less than or equal to x and (mid+1)^2 is greater than x
if mid * mid <= x and (mid + 1) * (mid + 1) > x:
return mid
# If mid^2 is greater than x, adjust the right pointer
elif mid * mid > x:
right = mid - 1
# If mid^2 is less than x, adjust the left pointer
else:
left = mid + 1
# Return the result; the loop ensures we find the correct integer
return mid