Sqrt(x) - Complete Solution Guide
Sqrt(x) is LeetCode problem 69, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
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. For example, do not use 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 intege
Detailed Explanation
The core task here is to calculate the integer square root of a given non-negative integer `x`, always rounding down to the nearest whole number. For instance, if `x` is 4, we expect 2. If `x` is 8, where the true square root is approximately 2.828, we still return 2 because the problem explicitly states we must round down. The returned value also needs to be non-negative.
Solution Approach
The provided solution cleverly employs binary search to navigate the problem's constraints and achieve an efficient result. Instead of trying to calculate the square root directly, we reframe the problem as 'find the largest integer `k` such that `k * k <= x`.' Our search space for `k` is naturally bounded: the smallest possible non-negative square root is 0, and the largest it could possibly be is `x` itself (e.g., `sqrt(1) = 1`, `sqrt(0) = 0`). So, for `x > 0`, we set our search range from `left = 1` to `right = x`.
Step-by-Step Algorithm
- Step 1: Initialize `left` to 1 and `right` to `x` (or `x/2` for optimization). Initialize `ans` to 0.
- Step 2: While `left` is less than or equal to `right`, calculate the middle element `mid`.
- Step 3: Check if `mid * mid <= x` (or equivalently, `mid <= x / mid` to prevent overflow). If true, update `ans` to `mid` and move `left` to `mid + 1`. Otherwise, move `right` to `mid - 1`.
- Step 4: Repeat Step 2 and 3 until the loop terminates. The final value of `ans` is the integer square root.
Key Insights
- Reframing the square root problem as a search for the largest integer `k` such that `k^2 <= x` immediately highlights binary search as an optimal strategy, transforming a direct computation into an efficient search over a naturally ordered range (`[1, x]` for `x > 0`).
- Employing `mid <= x // mid` instead of `mid * mid <= x` is a crucial defensive programming choice. It elegantly prevents potential integer overflow issues when `mid` is large, especially in languages with fixed-size integers, ensuring the solution remains robust for very large input values of `x` without sacrificing correctness.
- The binary search's update logic, where `ans` is stored when `mid^2 <= x` and then `left` is pushed higher (`left = mid + 1`), intrinsically satisfies the 'round down' requirement. This ensures that `ans` consistently tracks the largest integer whose square does not exceed `x`, converging directly to the problem's desired output.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Math, Binary Search.
Companies
Asked at: Accenture, Adobe, Amazon, Apple, Bloomberg, Citadel, Goldman Sachs, Grammarly, Infosys, Meta, Microsoft, SAP, Samsung, TikTok, Uber, Yahoo, tcs.