Skip to content

added solution to leetcode 648 #3939

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 44 additions & 39 deletions dsa-solutions/gfg-solutions/Easy problems/Square-Root.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ id: square-root
title: Square Root
sidebar_label: Square-Root
tags:
- Math
- Binary Search
- Math
- Binary Search
description: "This document provides solutions to the problem of finding the Square Root of an integer."
---

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

**Constraints**

- `1 ≤ x ≤ 10^7`

## Solution
Expand Down Expand Up @@ -128,25 +129,27 @@ public:

```javascript
class Solution {
floorSqrt(x) {
if (x === 0 || x === 1) {
return x;
}
let start = 1, end = x, ans = 0;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (mid * mid === x) {
return mid;
}
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else {
end = mid - 1;
}
}
return ans;
floorSqrt(x) {
if (x === 0 || x === 1) {
return x;
}
let start = 1,
end = x,
ans = 0;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (mid * mid === x) {
return mid;
}
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else {
end = mid - 1;
}
}
return ans;
}
}
```

Expand All @@ -155,25 +158,27 @@ class Solution {

```typescript
class Solution {
floorSqrt(x: number): number {
if (x === 0 || x === 1) {
return x;
}
let start = 1, end = x, ans = 0;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (mid * mid === x) {
return mid;
}
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else {
end = mid - 1;
}
}
return ans;
floorSqrt(x: number): number {
if (x === 0 || x === 1) {
return x;
}
let start = 1,
end = x,
ans = 0;
while (start <= end) {
let mid = Math.floor((start + end) / 2);
if (mid * mid === x) {
return mid;
}
if (mid * mid < x) {
start = mid + 1;
ans = mid;
} else {
end = mid - 1;
}
}
return ans;
}
}
```

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

**Time Complexity:** $O(log N)$
**Auxiliary Space:** $O(1)$
**Auxiliary Space:** $O(1)$
Loading
Loading