LC 50
LeetCode 50 · Medium

Pow(x, n)

Implement x raised to the integer power n.

Math → · High frequency · Solve on LeetCode →

Try it

Step through the core mechanic. The simulator below runs the math shape this problem is built on.

Walk the pattern

No dedicated step-through for this one yet. The shape is Math — its pattern page has the interactive walkthrough, the reference implementation, and a five-problem progression that this problem sits inside.

The approach

Fast exponentiation by squaring: x^n = (x^(n/2))², with an extra ×x for odd n. Handle negative n by inverting x. Halving the exponent each step gives O(log n).

AspectValue
PatternMath
Recognise it byExponentiation in O(log n).
Time complexityO(log n)
Space complexityO(1)
DifficultyMedium

Who asks it

Companies known to ask this problem, from public LeetCode company-tag aggregations. A signal of where to expect it, not a guarantee.