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
+44-39Lines changed: 44 additions & 39 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,6 +38,7 @@ 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
+
41
42
-`1 ≤ x ≤ 10^7`
42
43
43
44
## Solution
@@ -128,25 +129,27 @@ public:
128
129
129
130
```javascript
130
131
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
+
}
149
150
}
151
+
return ans;
152
+
}
150
153
}
151
154
```
152
155
@@ -155,25 +158,27 @@ class Solution {
155
158
156
159
```typescript
157
160
classSolution {
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;
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
+
}
176
179
}
180
+
returnans;
181
+
}
177
182
}
178
183
```
179
184
@@ -185,4 +190,4 @@ class Solution {
185
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.
Copy file name to clipboardExpand all lines: dsa-solutions/lc-solutions/0200-0299/0240-search-a-2d-matrix-II.md
+15-18Lines changed: 15 additions & 18 deletions
Original file line number
Diff line number
Diff line change
@@ -69,14 +69,15 @@ Output: false
69
69
### Intuition and Approach
70
70
71
71
To search for a value in this matrix efficiently, we can utilize the properties of the matrix. Since the matrix is sorted both row-wise and column-wise, we can start our search from the top-right corner of the matrix. From here, we have two options:
72
+
72
73
1. If the target is greater than the current value, move downwards.
73
74
2. If the target is less than the current value, move leftwards.
74
75
75
76
This approach ensures that we eliminate one row or one column in each step, leading to an efficient search.
By leveraging the sorted properties of the matrix, we can search for the target value efficiently using a greedy approach. This involves starting from the top-right corner and adjusting our search direction based on the current value.
0 commit comments