Kth root using binary search

output is not comming ???

@hg11110000,

  • Line 19: if(r==n) x=m;
  • Correction: if(r==n) {x=m; break;}

Also this code won’t work for large n, because for instance, at the very first iteration of binary search loop, you are trying to calculate (n/2)^k, so if n = 1000000000000000 and k = 10000, the result will be too large for any data type to store, and thus overflow to give wrong answer.

if i use long long ??
then also answer is not comming

@hg11110000,
Obviously, you are trying to calculate (10^15)^(10^4), it won’t fit in long long as well.

Then what to do now ? For square root using binary search we use to follow this approach only ???

@hg11110000,

  • Hint: You don’t need to calculate complete m^k in order to tell whether it will be greater than n or not.

can you please tell me the full solution please :((

@hg11110000,

Code.

I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.

On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.