Implement pow(x, n), which calculates x raised to the power n (i.e., xn).
Input: x = 2.00000, n = 10
Output: 1024.00000
Input: x = 2.10000, n = 3
Output: 9.26100
Input: x = 2.00000, n = -2
Output: 0.25000
Explanation: 2^(-2) = 1/2^2 = 1/4 = 0.25
-100.0 < x < 100.0-2^31 <= n <= 2^31 - 1n is an integerx is not zero or n > 0-10^4 <= x^n <= 10^4Multiply x by itself n times.
public double myPow(double x, int n) {
if (n == 0) return 1;
double result = 1;
long N = Math.abs((long) n);
for (long i = 0; i < N; i++) {
result *= x;
}
return n < 0 ? 1 / result : result;
}
Time Complexity: O(n) - Too slow for large n Space Complexity: O(1)
This approach will Time Limit Exceeded for large values of n.
Key Insight: Use the property that xn = (xn/2)2
This reduces the problem size by half at each step!
Recursive Formula:
Example: Calculate 210
2^10 = (2^5)^2
2^5 = 2 × (2^4) = 2 × (2^2)^2
2^4 = (2^2)^2
2^2 = (2^1)^2
2^1 = 2
Working backwards:
2^2 = 4
2^4 = 16
2^5 = 2 × 16 = 32
2^10 = 32 × 32 = 1024
Only 5 multiplications instead of 10!
We can also think of this using binary representation of n.
For n = 10 = 10102 (binary):
10 = 8 + 2 = 2^3 + 2^1
x^10 = x^(8+2) = x^8 × x^2
Algorithm:
Example: 35 where 5 = 1012
Binary of 5: 1 0 1
Position: 2 1 0
result = 1, base = 3
Bit 0 (1): result = result × base = 1 × 3 = 3
base = base² = 9
Bit 1 (0): Skip multiplication
base = base² = 81
Bit 2 (1): result = result × base = 3 × 81 = 243
Answer: 243
Binary Exponentiation:
Time Complexity: O(log n)
Space Complexity:
class Solution {
public double myPow(double x, int n) {
// Handle edge cases
if (n == 0) return 1.0;
if (x == 0) return 0.0;
if (x == 1) return 1.0;
if (x == -1) return n % 2 == 0 ? 1.0 : -1.0;
// Convert to long to handle Integer.MIN_VALUE
long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}
double result = 1.0;
double currentProduct = x;
// Binary exponentiation
while (N > 0) {
// If current bit is 1, multiply result
if ((N & 1) == 1) {
result *= currentProduct;
}
// Square the base for next bit
currentProduct *= currentProduct;
// Move to next bit
N >>= 1;
}
return result;
}
}
class Solution {
public double myPow(double x, int n) {
long N = n;
if (N < 0) {
x = 1 / x;
N = -N;
}
return fastPow(x, N);
}
private double fastPow(double x, long n) {
if (n == 0) return 1.0;
double half = fastPow(x, n / 2);
if (n % 2 == 0) {
return half * half;
} else {
return half * half * x;
}
}
}
def myPow(x: float, n: int) -> float:
# Handle edge cases
if n == 0:
return 1.0
if x == 0:
return 0.0
if x == 1:
return 1.0
if x == -1:
return 1.0 if n % 2 == 0 else -1.0
# Handle negative power
N = abs(n)
if n < 0:
x = 1 / x
result = 1.0
current_product = x
# Binary exponentiation
while N > 0:
# If current bit is 1
if N & 1:
result *= current_product
# Square the base
current_product *= current_product
# Right shift
N >>= 1
return result
def myPow(x: float, n: int) -> float:
def fast_pow(base: float, exp: int) -> float:
if exp == 0:
return 1.0
half = fast_pow(base, exp // 2)
if exp % 2 == 0:
return half * half
else:
return half * half * base
N = abs(n)
result = fast_pow(x, N)
return result if n >= 0 else 1 / result
Input: x = 2, n = -2
Output: 0.25
Explanation: 2^(-2) = 1/4 = 0.25
Input: x = 2, n = 0
Output: 1
Explanation: Any number to power 0 is 1
Input: x = 2, n = Integer.MIN_VALUE (-2147483648)
Must convert to long because -Integer.MIN_VALUE overflows!
Input: x = -2, n = 3
Output: -8
Input: x = -2, n = 4
Output: 16
This problem demonstrates:
Integer.MIN_VALUE (using int instead of long)long instead of int 2^10
/ \
2^5 2^5
/ \
2^2 2^2
/ \
2^1 2^1
/
2^0
Each level halves the exponent → O(log n) depth
n = 10 = 1010₂
Step 1: Check bit 0 (0): skip, square base: 2² = 4
Step 2: Check bit 1 (1): multiply result (1 × 4 = 4), square base: 4² = 16
Step 3: Check bit 2 (0): skip, square base: 16² = 256
Step 4: Check bit 3 (1): multiply result (4 × 256 = 1024)
Result: 1024
Q: Can you do better than O(log n)?
Q: What about very large exponents (modular exponentiation)?
(a × b) % m = ((a % m) × (b % m)) % m propertyQ: How to handle matrix exponentiation?
Q: What if n is a floating point number?
Comparison:
This is a 1000× speedup!
| Approach | Time | Space | Notes | |----------|------|-------|-------| | Brute Force | O(n) | O(1) | TLE for large n | | Binary (Iterative) | O(log n) | O(1) | Optimal ⭐ | | Binary (Recursive) | O(log n) | O(log n) | Call stack space |
Recommendation: Use iterative binary exponentiation for best space complexity.