-
Notifications
You must be signed in to change notification settings - Fork 0
/
033. square root using binary search.cpp
58 lines (50 loc) · 1.53 KB
/
033. square root using binary search.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
69. Sqrt(x) - LeetCode
Given a non-negative integer x, compute and return the square root of x.
Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned.
Note: You are not allowed to use any built-in exponent function or operator, such as pow(x, 0.5) or x ** 0.5.
Input: x = 8
Output: 2
Explanation:
The square root of 8 is 2.82842..., and since the decimal part is truncated, 2 is returned.
Code:
class Solution {
private:
long long int binarySearch(int num) {
int start = 0;
int end = num;
long long int mid = start + (end - start) / 2;
long long int ans = -1;
while(start <= end) {
long long int square = mid * mid;
if(square == num) {
return mid;
}
else if(square > num) {
end = mid - 1;
}
else {
ans = mid; //storing probable answer of sqrt
start = mid + 1;
}
mid = start + (end - start) / 2;
}
return ans;
}
public:
int mySqrt(int x) {
return binarySearch(x);
}
};
#if decimal-part(precision) of the square-root if required:
Code:
double decimalPart(int n, int precision, int intPart) {
double factor = 1;
double ans = intPart;
for(int i=0;i<precision;i++) {
factor = factor / 10;
for(double j=ans;j*j<n;j=j+factor) {
ans = j;
}
}
return ans;
}