I have understood the code but I am not able understand that how did the complexity came as O(log(n)).
How did we get the value of complexity
@apoorvsingh27,
Say we need to calulate for power(x,y)
We first calculate and store power(x,y/2).
Now if y is even. Our final answer will just the square of [power(x,y/2)] this value.
And if y is odd, our final answer will be square of [power(x,y/2)] * x.
And now if we need to calculate for power(x,y/2).
Here if y/2 is odd and if y/2 is even the same process follows.
In this approach we are dividing our sub problems by 2.
power(x,y) -> power(x,y/2) -> power(x,y/4) -> power(x,y/8) -> power(x,y/16) -> … ->power(x,1)
Hence the complexity will be O(log(n))