-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsqrt(x).cpp
More file actions
39 lines (33 loc) · 805 Bytes
/
sqrt(x).cpp
File metadata and controls
39 lines (33 loc) · 805 Bytes
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
//problem number 69
//Sqrt(x)
// How to find square root of an number without using any library
class Solution {
public:
int mySqrt(int x) {
//using built in function
// int ans=sqrt(x);
// return ans;
//without using built in function
long long int mid,ans;
long long int start=0;
long long int end=x;
while(start<=end)
{
mid=(start+end)/2;
if((mid*mid)==x)
{
ans=mid;
return ans;
}
else if((mid*mid)>x)
{
end=mid-1;
}
else
{
start=mid+1;
}
}
return end;
}
};