This is what I have implemented https://ide.codingblocks.com/s/268332
But I am not able to pass one test case, it is showing wrong error.
This is what I have implemented https://ide.codingblocks.com/s/268332
But I am not able to pass one test case, it is showing wrong error.
Hey @anugrahrastogi
just a change
System.out.println( (int)(Math.pow(n, (float)(1.0/k))));
instead of System.out.println( (int) Math.floor(Math.pow(n, 1.0/k)));
But this question is the application of binary search.
I would say that this question can be done with the help of binary search
I have 2 questions:
Q1 .How does adding float solves the problem?
Answer :
try for this input
1 1000000000000 6
your code gives 99
but correct output 100
pow fun default arguments type double
size of double is 64 bit
1.0/k in 64 bit
so, we needed type cast
otherwise ,some larger input you may be got wrong answer
Q2 . how to solve this problem using binary search?
We will aplly binary search in this problem. For every possible mid obtained by using binary search we will check of it is the best suitable candidate or not for becoming the Kth root and then we reduce the search space of the binary search according to the mid value. If mid^k is greater than N then we will find the best suitable value from left to mid-1 otherwise we will find much larger value by finding it from mid+1 to right.