fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. // your code goes here
  13. }
  14. }
  15.  
  16. class Solution {
  17.  
  18. public int pow(int power , int num , int lim){
  19. int ans = 1;
  20. while(power != 0){
  21. if(power%2 == 0){
  22. num = num*num;
  23. power = power/2;
  24. }else{
  25. ans = ans*num;
  26. power = power -1;
  27. }
  28. if(num > lim)return -1;
  29. if(ans > lim)return -1;
  30. }
  31. return ans;
  32. }
  33.  
  34. public int nthRoot(int n, int m) {
  35. // code here
  36.  
  37. if(m == 0)return 0;
  38. if(m == 1)return 1;
  39.  
  40. int ans = -1;
  41. int hi = m ;
  42. int lo = 2;
  43.  
  44. while(lo <= hi){
  45.  
  46. int mid = lo + (hi - lo)/2;
  47.  
  48. int raised = pow(n , mid , m);
  49. if(raised == m){
  50. ans = mid;
  51. break;
  52. }
  53. if(raised == -1){ // exceeded
  54. hi = mid - 1;
  55. }else{ //smaller
  56. lo = mid + 1;
  57. }
  58.  
  59. }
  60. return ans;
  61. }
  62. }
Success #stdin #stdout 0.07s 54692KB
stdin
Standard input is empty
stdout
Standard output is empty