|
| 1 | +--- |
| 2 | +id: square-root |
| 3 | +title: Square Root |
| 4 | +sidebar_label: Square-Root |
| 5 | +tags: |
| 6 | +- Math |
| 7 | +- Binary Search |
| 8 | +description: "This document provides solutions to the problem of finding the Square Root of an integer." |
| 9 | +--- |
| 10 | + |
| 11 | +## Problem |
| 12 | + |
| 13 | +Given an integer `x`, find the square root of `x`. If `x` is not a perfect square, then return the floor value of √x. |
| 14 | + |
| 15 | +### Examples |
| 16 | + |
| 17 | +**Example 1:** |
| 18 | + |
| 19 | +``` |
| 20 | +Input: x = 5 |
| 21 | +Output: 2 |
| 22 | +Explanation: Since 5 is not a perfect square, the floor of the square root of 5 is 2. |
| 23 | +``` |
| 24 | + |
| 25 | +**Example 2:** |
| 26 | + |
| 27 | +``` |
| 28 | +Input: x = 4 |
| 29 | +Output: 2 |
| 30 | +Explanation: Since 4 is a perfect square, its square root is 2. |
| 31 | +``` |
| 32 | + |
| 33 | +### Your Task |
| 34 | + |
| 35 | +You don't need to read input or print anything. The task is to complete the function `floorSqrt()` which takes `x` as the input parameter and returns its square root. Note: Try solving the question without using the sqrt function. The value of `x` ≥ 0. |
| 36 | + |
| 37 | +**Expected Time Complexity:** $O(log N)$ |
| 38 | +**Expected Auxiliary Space:** $O(1)$ |
| 39 | + |
| 40 | +**Constraints** |
| 41 | +- `1 ≤ x ≤ 10^7` |
| 42 | + |
| 43 | +## Solution |
| 44 | + |
| 45 | +### Intuition & Approach |
| 46 | + |
| 47 | +To find the square root of a number without using the built-in `sqrt` function, we can use binary search. This approach leverages the fact that the square root of `x` must lie between `0` and `x`. By repeatedly narrowing down the range using binary search, we can efficiently find the floor value of the square root. |
| 48 | + |
| 49 | +### Implementation |
| 50 | + |
| 51 | +<Tabs> |
| 52 | + <TabItem value="python" label="Python"> |
| 53 | + |
| 54 | +```python |
| 55 | +class Solution: |
| 56 | + def floorSqrt(self, x: int) -> int: |
| 57 | + if x == 0 or x == 1: |
| 58 | + return x |
| 59 | + start, end = 1, x |
| 60 | + ans = 0 |
| 61 | + while start <= end: |
| 62 | + mid = (start + end) // 2 |
| 63 | + if mid * mid == x: |
| 64 | + return mid |
| 65 | + if mid * mid < x: |
| 66 | + start = mid + 1 |
| 67 | + ans = mid |
| 68 | + else: |
| 69 | + end = mid - 1 |
| 70 | + return ans |
| 71 | +``` |
| 72 | + |
| 73 | + </TabItem> |
| 74 | + <TabItem value="java" label="Java"> |
| 75 | + |
| 76 | +```java |
| 77 | +class Solution { |
| 78 | + long floorSqrt(long x) { |
| 79 | + if (x == 0 || x == 1) { |
| 80 | + return x; |
| 81 | + } |
| 82 | + long start = 1, end = x, ans = 0; |
| 83 | + while (start <= end) { |
| 84 | + long mid = (start + end) / 2; |
| 85 | + if (mid * mid == x) { |
| 86 | + return mid; |
| 87 | + } |
| 88 | + if (mid * mid < x) { |
| 89 | + start = mid + 1; |
| 90 | + ans = mid; |
| 91 | + } else { |
| 92 | + end = mid - 1; |
| 93 | + } |
| 94 | + } |
| 95 | + return ans; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | + </TabItem> |
| 101 | + <TabItem value="cpp" label="C++"> |
| 102 | + |
| 103 | +```cpp |
| 104 | +class Solution { |
| 105 | +public: |
| 106 | + long long int floorSqrt(long long int x) { |
| 107 | + if (x == 0 || x == 1) |
| 108 | + return x; |
| 109 | + long long int start = 1, end = x, ans = 0; |
| 110 | + while (start <= end) { |
| 111 | + long long int mid = (start + end) / 2; |
| 112 | + if (mid * mid == x) |
| 113 | + return mid; |
| 114 | + if (mid * mid < x) { |
| 115 | + start = mid + 1; |
| 116 | + ans = mid; |
| 117 | + } else { |
| 118 | + end = mid - 1; |
| 119 | + } |
| 120 | + } |
| 121 | + return ans; |
| 122 | + } |
| 123 | +}; |
| 124 | +``` |
| 125 | +
|
| 126 | + </TabItem> |
| 127 | + <TabItem value="javascript" label="JavaScript"> |
| 128 | +
|
| 129 | +```javascript |
| 130 | +class Solution { |
| 131 | + floorSqrt(x) { |
| 132 | + if (x === 0 || x === 1) { |
| 133 | + return x; |
| 134 | + } |
| 135 | + let start = 1, end = x, ans = 0; |
| 136 | + while (start <= end) { |
| 137 | + let mid = Math.floor((start + end) / 2); |
| 138 | + if (mid * mid === x) { |
| 139 | + return mid; |
| 140 | + } |
| 141 | + if (mid * mid < x) { |
| 142 | + start = mid + 1; |
| 143 | + ans = mid; |
| 144 | + } else { |
| 145 | + end = mid - 1; |
| 146 | + } |
| 147 | + } |
| 148 | + return ans; |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | + </TabItem> |
| 154 | + <TabItem value="typescript" label="TypeScript"> |
| 155 | + |
| 156 | +```typescript |
| 157 | +class Solution { |
| 158 | + floorSqrt(x: number): number { |
| 159 | + if (x === 0 || x === 1) { |
| 160 | + return x; |
| 161 | + } |
| 162 | + let start = 1, end = x, ans = 0; |
| 163 | + while (start <= end) { |
| 164 | + let mid = Math.floor((start + end) / 2); |
| 165 | + if (mid * mid === x) { |
| 166 | + return mid; |
| 167 | + } |
| 168 | + if (mid * mid < x) { |
| 169 | + start = mid + 1; |
| 170 | + ans = mid; |
| 171 | + } else { |
| 172 | + end = mid - 1; |
| 173 | + } |
| 174 | + } |
| 175 | + return ans; |
| 176 | + } |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | + </TabItem> |
| 181 | +</Tabs> |
| 182 | + |
| 183 | +## Complexity Analysis |
| 184 | + |
| 185 | +The provided solutions efficiently find the floor value of the square root of a given integer `x` using binary search. This approach ensures a time complexity of $ O(log N) and an auxiliary space complexity of $O(1)$. The algorithms are designed to handle large values of `x` up to 10^7 efficiently without relying on built-in square root functions. |
| 186 | + |
| 187 | +**Time Complexity:** $O(log N)$ |
| 188 | +**Auxiliary Space:** $O(1)$ |
0 commit comments