Skip to content

Commit f375a4b

Browse files
authored
Merge pull request #3925 from komal-agarwal5/work1
Add Prev Pr
2 parents 1a321f3 + 3dcbf4e commit f375a4b

File tree

6 files changed

+223
-286
lines changed

6 files changed

+223
-286
lines changed

dsa-problems/gfg-problems/basic/problems.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ keywords:
88
- gfg problems problems
99
---
1010

11-
1211
export const problems = [
1312
{
1413
"problemName": "1. Inorder Traversal",

dsa-solutions/gfg-solutions/Easy problems/square-root.md

Lines changed: 39 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ id: square-root
33
title: Square Root
44
sidebar_label: Square-Root
55
tags:
6-
- Math
7-
- Binary Search
6+
- Math
7+
- Binary Search
88
description: "This document provides solutions to the problem of finding the Square Root of an integer."
99
---
1010

@@ -38,7 +38,6 @@ You don't need to read input or print anything. The task is to complete the func
3838
**Expected Auxiliary Space:** $O(1)$
3939

4040
**Constraints**
41-
4241
- `1 ≤ x ≤ 10^7`
4342

4443
## Solution
@@ -129,27 +128,25 @@ public:
129128
130129
```javascript
131130
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;
150149
}
151-
return ans;
152-
}
153150
}
154151
```
155152

@@ -158,27 +155,25 @@ class Solution {
158155

159156
```typescript
160157
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-
}
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;
179176
}
180-
return ans;
181-
}
182177
}
183178
```
184179

@@ -190,4 +185,4 @@ class Solution {
190185
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.
191186

192187
**Time Complexity:** $O(log N)$
193-
**Auxiliary Space:** $O(1)$
188+
**Auxiliary Space:** $O(1)$

0 commit comments

Comments
 (0)