Skip to content

Commit 6ac6ed6

Browse files
authored
Merge pull request #519 from thevijayshankersharma/sqrt-x
Add a Solution for Sqrt(x) (LeetCode Problem 69)
2 parents f9c7aaa + 841cc25 commit 6ac6ed6

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
---
2+
id: sqrt-x
3+
title: Sqrt(x) (LeetCode)
4+
difficulty: Easy
5+
sidebar_label: 0069-SqrtX
6+
topics:
7+
- Math
8+
- Binary Search
9+
---
10+
11+
12+
## Problem Description
13+
14+
| Problem Statement | Solution Link | LeetCode Profile |
15+
| :---------------- | :------------ | :--------------- |
16+
| [Merge Two Sorted Lists](https://leetcode.com/problems/sqrtx/) | [Merge Two Sorted Lists Solution on LeetCode](https://leetcode.com/problems/sqrtx/solutions/) | [VijayShankerSharma](https://leetcode.com/u/darkknight648/) |
17+
18+
## Problem Description
19+
20+
Given a non-negative integer `x`, return the square root of `x` rounded down to the nearest integer. The returned integer should be non-negative as well.
21+
22+
You must not use any built-in exponent function or operator.
23+
24+
### Examples
25+
26+
#### Example 1:
27+
28+
- **Input:** `x = 4`
29+
- **Output:** `2`
30+
- **Explanation:** The square root of 4 is 2, so we return 2.
31+
32+
#### Example 2:
33+
34+
- **Input:** `x = 8`
35+
- **Output:** `2`
36+
- **Explanation:** The square root of 8 is approximately 2.82842..., and since we round it down to the nearest integer, 2 is returned.
37+
38+
### Constraints:
39+
40+
- `0 <= x <= 2^31 - 1`
41+
42+
### Approach
43+
44+
To find the square root of a non-negative integer `x` without using built-in functions, we can use binary search within the range `[0, x]`.
45+
46+
1. Initialize variables `left` and `right` to represent the search range `[0, x]`.
47+
2. Perform binary search within this range, updating the `mid` value as `(left + right) / 2`.
48+
3. If `mid * mid` is greater than `x`, update `right` to `mid - 1`.
49+
4. If `mid * mid` is less than or equal to `x`, update `left` to `mid + 1`.
50+
5. Repeat steps 2-4 until `left` is greater than `right`.
51+
6. Return the value of `right`, which represents the largest integer whose square is less than or equal to `x`.
52+
53+
### Solution Code
54+
55+
#### Python
56+
57+
```
58+
class Solution(object):
59+
def mySqrt(self, x):
60+
if x < 2:
61+
return x
62+
left, right = 1, x
63+
while left <= right:
64+
mid = (left + right) // 2
65+
if mid * mid == x:
66+
return mid
67+
elif mid * mid < x:
68+
left = mid + 1
69+
else:
70+
right = mid - 1
71+
return right
72+
```
73+
74+
#### C++
75+
76+
```
77+
class Solution {
78+
public:
79+
int mySqrt(int x) {
80+
if (x == 0) {
81+
return 0;
82+
}
83+
84+
long left = 1, right = x;
85+
while (left <= right) {
86+
long mid = (left + right) / 2;
87+
if (mid * mid == x) {
88+
return mid;
89+
} else if (mid * mid < x) {
90+
left = mid + 1;
91+
} else {
92+
right = mid - 1;
93+
}
94+
}
95+
96+
return right;
97+
}
98+
};
99+
```
100+
101+
#### Java
102+
103+
```
104+
class Solution {
105+
public int mySqrt(int x) {
106+
if (x == 0) {
107+
return 0;
108+
}
109+
110+
long left = 1, right = x;
111+
while (left <= right) {
112+
long mid = (left + right) / 2;
113+
if (mid * mid == x) {
114+
return (int)mid;
115+
} else if (mid * mid < x) {
116+
left = mid + 1;
117+
} else {
118+
right = mid - 1;
119+
}
120+
}
121+
122+
return (int)right;
123+
}
124+
}
125+
```
126+
127+
### Conclusion
128+
129+
The "Sqrt(x)" problem can be efficiently solved using binary search within the range `[0, x]`. The provided solution code implements this approach in Python, C++, and Java, providing an optimal solution to the problem.

0 commit comments

Comments
 (0)