Fast Exponentiation Power in Log N in DSA C++ - Time & Space Complexity
We want to understand how fast the power calculation grows as the exponent gets bigger.
How does the number of steps change when we raise a number to a large power?
Analyze the time complexity of the following code snippet.
int fastPower(int base, int exponent) {
if (exponent == 0) return 1;
int half = fastPower(base, exponent / 2);
if (exponent % 2 == 0) {
return half * half;
} else {
return half * half * base;
}
}
This code calculates base raised to the exponent using repeated squaring.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that halve the exponent each time.
- How many times: About log2(exponent) times, because the exponent is divided by 2 each call.
Each step cuts the exponent roughly in half, so the number of steps grows slowly as exponent grows.
| Input Size (exponent) | Approx. Operations (recursive calls) |
|---|---|
| 10 | About 4 |
| 100 | About 7 |
| 1000 | About 10 |
Pattern observation: Doubling the exponent adds only one more step.
Time Complexity: O(log n)
This means the steps grow slowly, only increasing by one each time the exponent doubles.
[X] Wrong: "The function does one multiplication per exponent value, so it is O(n)."
[OK] Correct: The function splits the problem in half each time, so it does far fewer steps than the exponent itself.
Understanding this method shows you can improve simple tasks by clever thinking, a skill valued in coding challenges and interviews.
"What if we changed the function to use a loop instead of recursion? How would the time complexity change?"
Practice
Solution
Step 1: Understand simple exponentiation time
Simple repeated multiplication takes n multiplications for power n, so O(n) time.Step 2: Analyze fast exponentiation time
Fast exponentiation splits power by half each step, reducing steps to about log n, so O(log n) time.Final Answer:
It reduces the time complexity from O(n) to O(log n) -> Option BQuick Check:
Fast exponentiation = O(log n) [OK]
- Thinking it avoids multiplication
- Confusing time complexity with memory usage
- Assuming it only works for positive exponents
Solution
Step 1: Identify base case for exponent zero
Any number to the power 0 is 1, so if n == 0, return 1.Step 2: Check other options
Other options do not correctly represent the base case for power calculation.Final Answer:
if (n == 0) return 1; -> Option DQuick Check:
Base case n=0 returns 1 [OK]
- Returning 0 for n=1
- Confusing base case with input x values
- Returning n instead of 1 for base case
int fastPow(int x, int n) {
if (n == 0) return 1;
int half = fastPow(x, n / 2);
if (n % 2 == 0) return half * half;
else return half * half * x;
}
int main() {
std::cout << fastPow(2, 5);
return 0;
}Solution
Step 1: Trace recursive calls for fastPow(2,5)
fastPow(2,5) calls fastPow(2,2), which calls fastPow(2,1), which calls fastPow(2,0)=1.Step 2: Calculate values bottom-up
fastPow(2,1) = 1*1*2=2; fastPow(2,2) = 2*2=4; fastPow(2,5) = 4*4*2=32.Final Answer:
32 -> Option AQuick Check:
2^5 = 32 [OK]
- Calculating 2^5 as 16 by missing odd case
- Confusing multiplication order
- Forgetting base case returns 1
int fastPow(int x, int n) {
if (n == 0) return 1;
int half = fastPow(x, n / 2);
if (n % 2 == 0) return half * half;
else return half * half * n;
}Solution
Step 1: Check odd power multiplication
In the odd case, code multiplies by n instead of x, which is incorrect.Step 2: Confirm correct multiplication
It should multiply by x to account for the extra factor in odd powers.Final Answer:
Multiplying by n instead of x in the odd case -> Option AQuick Check:
Odd power multiply by x, not n [OK]
- Confusing n and x in multiplication
- Thinking base case missing causes this bug
- Ignoring negative power handling
Solution
Step 1: Check base case and modulo usage
Base case returns 1 for n=0, correct. Multiplications use modulo to avoid overflow.Step 2: Verify odd and even power handling
For odd n, multiply halfSq by x modulo m; for even n, return halfSq. int modPow(int x, int n, int m) { if (n == 0) return 1; int half = modPow(x, n / 2, m); int halfSq = (half * half) % m; if (n % 2 == 1) return (halfSq * x) % m; else return halfSq; } matches this logic correctly.Final Answer:
code correctly implements fast exponentiation with modulo -> Option CQuick Check:
Modulo applied after each multiplication [OK]
- Returning 0 for base case
- Using addition instead of multiplication
- Swapping odd and even cases
Ready to prove it?
Pass the test and this pattern moves unknown
