12. Power Of Numbers
Last updated
Was this helpful?
Last updated
Was this helpful?
The problem can be found at the following link:
I have use the modular exponentiation algorithm to efficiently calculate the power with a logarithmic time complexity.
Here's the step-by-step explanation of the algorithm:
Initialize a variable res
to 1, which will store the final result.
Enter a loop while n
is not zero:
Check if the least significant bit of n
is 1 by using the bitwise AND operator n & 1
.
If the bit is 1, multiply res
with a
modulo mod
and decrement n
by 1.
If the bit is 0, square a
modulo mod
and divide n
by 2.
Finally, return the calculated result res
.
For a more comprehensive understanding of binary exponentiation and modular exponentiation, I recommend reading this .
Time Complexity: The powerexpo
function operates in O(log N)
time complexity since we divide n
by 2 at each step until it becomes zero.
Auxiliary Space Complexity: O(1)
since we uses a constant amount of auxiliary space, independent of the input size,
For discussions, questions, or doubts related to this solution, please visit our . We welcome your input and aim to foster a collaborative learning environment.
If you find this solution helpful, consider supporting us by giving a ⭐ star
to the repository.