Fast Exponentiation Power in Log N in DSA Typescript - 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 calculate x to the power n quickly?
Analyze the time complexity of the following code snippet.
function fastPower(x: number, n: number): number {
if (n === 0) return 1;
const half = fastPower(x, Math.floor(n / 2));
if (n % 2 === 0) return half * half;
else return half * half * x;
}
This code calculates x raised to the power n using a fast method by splitting the problem in half each time.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls that split the exponent n by half each time.
- How many times: About log base 2 of n times, because n is divided by 2 each call.
Each time we call the function, the exponent gets cut in half, so the number of steps grows slowly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4 recursive calls |
| 100 | About 7 recursive calls |
| 1000 | About 10 recursive calls |
Pattern observation: Doubling n adds only one more step, so growth is very slow compared to n itself.
Time Complexity: O(log n)
This means the number of steps grows slowly as the exponent gets bigger, making it very efficient.
[X] Wrong: "Calculating power always takes n steps because we multiply x n times."
[OK] Correct: This method uses splitting and recursion to reduce steps to about log n, not n, so it is much faster.
Understanding this fast power method shows you can improve simple tasks by clever thinking, a skill valued in interviews and real projects.
"What if we changed the method to use a loop instead of recursion? How would the time complexity change?"
Practice
Fast Exponentiation to calculate a^b compared to the simple repeated multiplication?Solution
Step 1: Understand simple exponentiation complexity
Simple repeated multiplication calculatesa^bby multiplyingaby itselfbtimes, which takes O(b) time.Step 2: Analyze fast exponentiation complexity
Fast exponentiation uses the idea of squaring and reduces the number of multiplications to O(log b), making it much faster for large exponents.Final Answer:
It reduces the time complexity from linear to logarithmic in terms of exponent. -> Option AQuick Check:
Fast exponentiation = O(log n) time [OK]
- Thinking it avoids multiplication
- Assuming it uses more memory
- Believing it only works for small exponents
a and b?Solution
Step 1: Check parameter types
The function needs two inputs: baseaand exponentb, both numbers.Step 2: Check return type
The function returns the result ofa^b, which is a number.Final Answer:
function fastPower(a: number, b: number): number -> Option DQuick Check:
Correct signature = function fastPower(a: number, b: number): number [OK]
- Using string types for numbers
- Missing one parameter
- Returning void instead of number
function fastPower(a: number, b: number): number {
if (b === 0) return 1;
let half = fastPower(a, Math.floor(b / 2));
if (b % 2 === 0) return half * half;
else return half * half * a;
}
console.log(fastPower(2, 5));Solution
Step 1: Trace recursive calls for b=5
fastPower(2,5) calls fastPower(2,2), which calls fastPower(2,1), which calls fastPower(2,0)=1.Step 2: Calculate results bottom-up
fastPower(2,1) = 1*1*2 = 2
fastPower(2,2) = 2*2 = 4
fastPower(2,5) = 4*4*2 = 32Final Answer:
32 -> Option CQuick Check:
2^5 = 32 [OK]
- Confusing multiplication order
- Forgetting to multiply by base when exponent is odd
- Incorrect base case handling
function fastPower(a: number, b: number): number {
if (b === 0) return 0;
let half = fastPower(a, Math.floor(b / 2));
if (b % 2 === 0) return half * half;
else return half * half * a;
}Solution
Step 1: Check base case correctness
When exponent is 0,a^0should return 1, but code returns 0, which is incorrect.Step 2: Identify fix for base case
Changing return value in base case from 0 to 1 fixes the function to correctly compute powers.Final Answer:
Change base case return from 0 to 1 -> Option BQuick Check:
Base case must return 1 [OK]
- Returning 0 for exponent 0
- Changing recursive call incorrectly
- Removing multiplication for odd exponent
(3^13) % 7 efficiently using fast exponentiation with modulo to avoid large numbers. Which modification to the fastPower function is correct?Solution
Step 1: Understand modulo property in exponentiation
To avoid large intermediate values, modulo must be applied after every multiplication step.Step 2: Modify function to apply modulo after multiplications
Applying modulo afterhalf * halfandhalf * half * akeeps numbers small and correct.Final Answer:
Apply modulo after each multiplication: (half * half) % mod and (half * half * a) % mod -> Option AQuick Check:
Modulo after each multiply keeps numbers small [OK]
- Applying modulo only once at end
- Applying modulo only to base
- Ignoring modulo for small exponents
Ready to prove it?
Pass the test and this pattern moves unknown
