Skip to content

Commit eae83d8

Browse files
committed
updt
1 parent ef1b2b7 commit eae83d8

File tree

5 files changed

+227
-281
lines changed

5 files changed

+227
-281
lines changed

dsa-solutions/gfg-solutions/Easy problems/Square-Root.md

Lines changed: 44 additions & 39 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,6 +38,7 @@ 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+
4142
- `1 ≤ x ≤ 10^7`
4243

4344
## Solution
@@ -128,25 +129,27 @@ public:
128129
129130
```javascript
130131
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;
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+
}
149150
}
151+
return ans;
152+
}
150153
}
151154
```
152155

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

156159
```typescript
157160
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;
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+
}
176179
}
180+
return ans;
181+
}
177182
}
178183
```
179184

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

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

0 commit comments

Comments
 (0)