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