Power(O(logn)) prblm


plz check up with my code

Anshul, the approach you are using in your code is not working for all possible test cases, Like for eg, If n=3 and p=3, answer should be 27, but your answer is coming out as 12, So the basic approach you will follow up is that of bitmasking only, you can try to implement the following logic,

int ans=1;
while(P>0)
{
if(P&1)
{
ans=ansN;
}
P=P>>1;
N=N
N;
}
return ans;

This is the fast power method…