You are given a floating-point number x and an integer n. Compute x raised to the power n — that is, x^n — without calling your language's built-in power function.
The exponent can be negative or zero:
x^0 = 1 for any valid input.x^-n = 1 / x^n.Return the result as a floating-point number.
-n overflows: 2^31 does not fit in a 32-bit int, so negating -2^31 in place is undefined or wraps around. Copy the exponent into a 64-bit variable (or a plain number in Python/JavaScript) before negating.x^-n = (1/x)^n: replace x with 1 / x, make the exponent positive, and run the exact same loop. The statement guarantees you never face x = 0 with a negative n, so the division is always safe.1 is the multiplicative identity — the empty product. Starting at x silently adds one extra factor, which corrupts every even exponent and makes x^0 return x instead of 1.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 2^-2 = 1 / 2^2 = 1/4 = 0.25
- -100.0 < x < 100.0 - -2^31 <= n <= 2^31 - 1 - n is an integer - Either x is not zero or n > 0 - -10^4 <= x^n <= 10^4
Computing x^n looks like a one-liner — until you notice n can be around two billion. Multiplying one step at a time is far too slow, and the fix is one of the most reusable tricks in all of computing: squaring your way up instead of counting your way up. This tutorial takes you from the obvious loop to binary exponentiation in O(log n).
x^(a+b) = x^a * x^b and x^-n = 1 / x^n.10 = 8 + 2 — which is exactly what lets squaring replace counting.In plain English: multiply x by itself n times — but do it fast enough that n ≈ 2 * 10^9 still finishes instantly. Formally: given a real x and an integer n (possibly negative or zero), return x^n, where x^-n = 1 / x^n and x^0 = 1.
Worked example — x = 2, n = 10
count up (brute): 2 → 4 → 8 → 16 → … → 1024 (10 multiplications) double up (fast): 2 → 4 → 16 → 256 → 65536 (4 squarings) n = 10 = 1010 in binary = 8 + 2 x^10 = x^8 · x^2 = 256 · 4 = 1024 ✓
Asking two or three sharp questions before typing signals that you design for the whole input space, not just the happy path.
“Can the exponent n be negative or zero?”
Yes to both. x^0 = 1, and a negative exponent flips the base: x^-n = (1/x)^n. Normalising the sign up front keeps the core loop clean.
“Can x be negative?”
Yes — a negative base with an odd exponent gives a negative result, but the algorithm itself never changes.
“Can x be 0 while n is negative?”
No — the statement guarantees either x is non-zero or n > 0, so you never divide by zero.
“What about n equal to the most negative 32-bit integer?”
This is the classic trap: -n overflows a 32-bit int because 2^31 does not fit. Copy n into a 64-bit variable before negating.
“How large can n get?”
Up to about 2.1 * 10^9. An O(n) loop is billions of multiplications — hopeless. That single number tells you the interviewer wants O(log n).
Before I code, a few clarifying questions.
Can n be negative or zero — and if negative, I will compute one over x to the positive power?
Since n can be around two billion, a linear loop is too slow, so I will use binary exponentiation for O(log n).
One edge case I want to flag: negating the most negative 32-bit integer overflows, so I will hold the exponent in a 64-bit variable.
From x^(a+b) = x^a * x^b, setting a = b gives x^(2k) = (x^k)^2. One squaring buys you what would otherwise cost k extra multiplications.
x^10 = (x^5)^2 one multiplication, exponent halved
When the exponent is odd you cannot halve it cleanly — so peel one factor off first: x^5 = x · x^4. Now the remaining exponent is even and squaring applies again. Alternate peel and square and the exponent collapses to zero in O(log n) steps.
Write n in binary: 10 = 1010. Each 1 bit says multiply the current square into the answer; each squaring moves you to the next bit. About 31 iterations cover the entire 32-bit range — that is the whole algorithm.
| Brute force | Optimal | |
|---|---|---|
| Time | O(n) | O(log n) |
| Space | O(1) | O(1) |
| n ≈ 2·10^9 | billions of steps — TLE | about 31 steps |
Full, runnable code for both approaches lives in the Approaches selector below.
Key takeaway
Halve the exponent, square the base. Binary exponentiation turns a linear count into a logarithmic ladder, and the same skeleton powers modular arithmetic, matrix exponentiation, and fast doubling everywhere.
if n < 0: x = 1/x, e = -n (in 64 bits) else e = n
result = 1
while e > 0:
if e is odd: result = result * x
x = x * x
e = e / 2 (integer division)
return result