Skip to content

Commit fada25a

Browse files
authored
Merge pull request #3918 from kosuri-indu/err/pr-score-descreased
Concern Regarding Score Reduction and Disappearance of Pull Requests
2 parents ef1b2b7 + e7e09d6 commit fada25a

File tree

2 files changed

+194
-195
lines changed

2 files changed

+194
-195
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,193 +1,193 @@
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+
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)$

0 commit comments

Comments
 (0)