-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSQRT.java
More file actions
43 lines (33 loc) · 762 Bytes
/
SQRT.java
File metadata and controls
43 lines (33 loc) · 762 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
40
41
42
43
package BinarySearch;
import java.util.Arrays;
/**
* Author - archit.s
* Date - 02/10/18
* Time - 9:56 AM
*/
public class SQRT {
public int sqrt(int a) {
if(a < 0){
return -1;
}
long low = 0;
long high = a;
while(low <= high){
long mid = low + (high - low)/2;
if(mid*mid <= a && (mid+1)*(mid+1) > a){
return (int)mid;
}
else if(mid*mid > a){
high = mid - 1;
}
else{
low = mid + 1;
}
}
return (int)high;
}
public static void main(String[] args) {
int[] a = {1,3,4};
System.out.println(Arrays.binarySearch(a, 2));
}
}