-
Notifications
You must be signed in to change notification settings - Fork 1
/
sqrtofanInt.java
46 lines (42 loc) · 1.28 KB
/
sqrtofanInt.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
46
package striver.binarysearch;
public class sqrtofanInt {
// returning 0 if ans == 0
// returning 1 if ans == m
// returining 2 if ans => m
public static int func1(long n, long m, long mid) {
long ans = 1;
for (int i = 1; i <= n; i++) {
ans = ans * mid;
if (ans > m) return 2;
}
if (ans == m) return 1;
return 0;
}
public static long func2(long n, long m) {
if (m == 0) return 0;
// Handling edge case where m is 0
if (n == 0 || m == 1) return m;
// Handling edge cases where n is 0 or m is 1
long low = 1;
long high = m;
while (low <= high) {
long mid = low + (high - low) / 2;
long midN = func1(n, m, mid);
if (midN == 1) {
return mid;
} else if (midN == 0) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high;
}
// need a hard drive revision for this questionnnn
public static void main(String[] args) {
long n = 2;
long m = 10;
long ans = func2(n, m);
System.out.println("The square root of this number = " + ans);
}
}