-
Notifications
You must be signed in to change notification settings - Fork 215
/
Copy pathsquare_root.java
44 lines (38 loc) · 1.19 KB
/
square_root.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
/* Square root of a number
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.
Examples:
Input: x = 36
Output: 6
Explanation: The square root of 36 is 6, so we return 6.
Input: x = 8
Output: 2
Explanation: The square root of 8 is 2.82842..., and since we round it down to the nearest integer,
2 is returned.
HOW TO SOLVE:
This is similar to finding perfect square of number but to return the square root.
We use Binary Search for this problem.
*/
public class SquareRoot{
public static int sqrt(int x){
int start = 1, end=x;
while(start<=end){
int mid = (start+end)/2;
if(x/mid==mid){
return mid;
}
else if(mid< x/mid){
start = mid+1;
}
else if(mid > x/mid){
end = mid-1;
}
}
return end;
}
public static void main(String[] args){
// you can also get input dynamically using Scanner class
int number = 36;
System.out.print(sqrt(number));
}
}