Skip to content
Veltoryx Technologies
ProProblem 72● MediumLeetCode #50 ↗4 min readUpdated 30 October 2026

Pow(x, n)

Calculate x raised to the power n in logarithmic time using binary exponentiation and integer overflow protection.

01The problem

Implement pow(x, n), which calculates x raised to the power n (i.e., x^n).

InputOutputWhy
x = 2.00000, n = 101024.000002^10 = 1024
x = 2.10000, n = 39.261002.1^3 = 9.261
x = 2.00000, n = -20.250002^(-2) = 1 / (2^2) = 1 / 4 = 0.25

Constraints: -100.0 < x < 100.0, -2^31 <= n <= 2^31 - 1, n is an integer. Either x is not zero or n > 0. Output is within [-10^4, 10^4].

Loading the rest of the lesson…