You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: dsa-solutions/gfg-solutions/Easy problems/square-root.md
+39-44Lines changed: 39 additions & 44 deletions
Original file line number
Diff line number
Diff line change
@@ -3,8 +3,8 @@ id: square-root
3
3
title: Square Root
4
4
sidebar_label: Square-Root
5
5
tags:
6
-
- Math
7
-
- Binary Search
6
+
- Math
7
+
- Binary Search
8
8
description: "This document provides solutions to the problem of finding the Square Root of an integer."
9
9
---
10
10
@@ -38,7 +38,6 @@ You don't need to read input or print anything. The task is to complete the func
38
38
**Expected Auxiliary Space:** $O(1)$
39
39
40
40
**Constraints**
41
-
42
41
-`1 ≤ x ≤ 10^7`
43
42
44
43
## Solution
@@ -129,27 +128,25 @@ public:
129
128
130
129
```javascript
131
130
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
-
}
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;
150
149
}
151
-
return ans;
152
-
}
153
150
}
154
151
```
155
152
@@ -158,27 +155,25 @@ class Solution {
158
155
159
156
```typescript
160
157
classSolution {
161
-
floorSqrt(x:number):number {
162
-
if (x===0||x===1) {
163
-
returnx;
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
-
returnmid;
172
-
}
173
-
if (mid*mid<x) {
174
-
start=mid+1;
175
-
ans=mid;
176
-
} else {
177
-
end=mid-1;
178
-
}
158
+
floorSqrt(x:number):number {
159
+
if (x===0||x===1) {
160
+
returnx;
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
+
returnmid;
167
+
}
168
+
if (mid*mid<x) {
169
+
start=mid+1;
170
+
ans=mid;
171
+
} else {
172
+
end=mid-1;
173
+
}
174
+
}
175
+
returnans;
179
176
}
180
-
returnans;
181
-
}
182
177
}
183
178
```
184
179
@@ -190,4 +185,4 @@ class Solution {
190
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.
0 commit comments