← All problems

Pow(x, n) (fast exponentiation)

Medium mathrecursionbinary-exponentiation LeetCode ↗
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.

Hints (0/3 revealed)

Log in to reveal hints progressively.

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.