-
Notifications
You must be signed in to change notification settings - Fork 0
/
FindSqrt.java
45 lines (39 loc) · 1.1 KB
/
FindSqrt.java
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
class Solution {
public int mySqrt(int x) {
// // handling edge cases;
// if(x==0)
// return 0;
// if(x==1)
// return 1;
// int prev =0;
// for(int i=0;i<x/2+1;i++){
// if(i*i==x){
// prev = i;
// break;
// }
// else if(i*i<x)
// prev = i;
// else
// break;
// }
// return prev;
// above solution giving tle we can use binary search for it;
// min will be zero and max will be integer max;
// handling edge cases;
if(x==0)
return 0;
int si =0;
int ei = Integer.MAX_VALUE;
while(true){
int mid = (si+ei)/2;
// here x/mid because mid and x will give the square
if(mid>(x/mid))
ei = mid-1;
else{
if (mid + 1 > x/(mid + 1))
return mid;
si = mid+1;
}
}
}
}