-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFind_Nth_root_of_M.java
40 lines (37 loc) · 959 Bytes
/
Find_Nth_root_of_M.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
// { Driver Code Starts
//Initial Template for Java
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while (T-- > 0) {
String s = br.readLine().trim();
String[] S1 = s.split(" ");
int n = Integer.parseInt(S1[0]);
int m = Integer.parseInt(S1[1]);
Solution ob = new Solution();
int ans = ob.NthRoot(n, m);
System.out.println(ans);
}
}
}
class Solution {
public int NthRoot(int n, int m) {
int low = 1, high = m;
while (low <= high) {
int mid = (low + high) / 2;
int nthPow = (int) Math.pow(mid, n);
if (nthPow == m) {
return mid;
} else if (nthPow > m) {
high = mid - 1;
} else {
low = mid + 1;
}
}
return -1;
}
}