← All problems
Pow(x, n) (fast exponentiation)
Compute x raised to the power n in O(log n) time, where n may be negative. Handle the n = -2^31 edge case without overflow.
Complexity
Time O(log n), Space O(log n) / O(1)
Common pitfalls
- O(n) repeated multiplication
- n = -2^31 edge in fixed-width langs
Your solution
Log in to save your code and track progress.