12. Power Of Numbers
The problem can be found at the following link: Question Link
My Approach
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
resto 1, which will store the final result.Enter a loop while
nis not zero:Check if the least significant bit of
nis 1 by using the bitwise AND operatorn & 1.If the bit is 1, multiply
reswithamodulomodand decrementnby 1.If the bit is 0, square
amodulomodand dividenby 2.
Finally, return the calculated result
res.
For a more comprehensive understanding of binary exponentiation and modular exponentiation, I recommend reading this article.
Time and Auxiliary Space Complexity
Time Complexity: The
powerexpofunction operates inO(log N)time complexity since we dividenby 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,
Code (C++)
class Solution {
public:
long long mod = 1e9+7;
long long powerexpo(long long a, long long n) {
long long res = 1LL;
while (n) {
if (n & 1) {
res = (res % mod * a % mod) % mod;
--n;
} else {
a = (a % mod * a % mod) % mod;
n >>= 1;
}
}
return res;
}
long long power(int N, int R) {
return powerexpo(N, R);
}
};Contribution and Support
For discussions, questions, or doubts related to this solution, please visit our discussion section. 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 getlost01/gfg-potd repository.
Last updated
Was this helpful?