Skip to content
AI360Xpert
Beta
Difficulty: MediumMath & Geometry

Pow(x, n)

Problem in Plain English

Compute x raised to the integer power n, handling negative exponents through reciprocals and full 32-bit exponents with only logarithmically many multiplications.

Problem Statement

Implement pow(x, n): raise the base x to the integer power n and return the result as a floating-point number.

The exponent may be negative, meaning the reciprocal (so x−2=1/x2x^{-2} = 1 / x^2), and n can stretch across the whole 32-bit range, where multiplying x by itself n times is far too slow. Note that negating the smallest 32-bit value overflows it, so widen n before flipping its sign.

Constraints

  • −100<x<100-100 < x < 100.
  • −231≤n≤231−1-2^{31} \leq n \leq 2^{31}-1.
  • n is an integer.
  • Either x is nonzero or n is positive.
  • −104≤xn≤104-10^4 \leq x^n \leq 10^4.

Examples

Example 1

Input
x = 2.00000, n = 10
Output
1024.00000

Multiplying 2 by itself ten times yields 1024.

Example 2

Input
x = 2.10000, n = 3
Output
9.26100

Three factors of 2.1 give 2.1×2.1×2.1=9.2612.1 \times 2.1 \times 2.1 = 9.261. Floating-point arithmetic may return a nearby representation.

Example 3

Input
x = 2.00000, n = -2
Output
0.25000

A negative exponent uses the reciprocal: 2−2=(1/2)2=0.252^{-2} = (1/2)^2 = 0.25.

Example 4

Input
x = -3.00000, n = 0
Output
1.00000

Zero factors form the empty product, one. Neither loop performs any multiplication.

Example 5

Input
x = -1.00000, n = -2147483648
Output
1.00000

After reciprocating the base, the positive magnitude 2147483648 is even. An even number of factors of -1 gives 1; Java must widen before negating the exponent.

Intuition

Exponentiation counts equal factors. Example 1 asks for ten factors of 2, but we can form powers 2, 4, 16 and 256 by repeatedly squaring. Since ten equals eight plus two, multiplying 256 by 4 produces 1024. A negative exponent changes the base to its reciprocal first; exponent zero uses the empty product, one.

Approaches

Repeated Multiply

Solution Details

Reveal Repeated Multiply: intuition, complexity, and code

Hints

Hint 1
Group equal factors by squaring: ten factors of 2 can be assembled from powers with exponents eight and two.
Hint 2
If the remaining exponent is odd, consume one factor before halving; if it is even, square the base and halve directly.
Hint 3
Maintain ans times base raised to exp as the normalized target power. Reciprocate for negative n and widen the Java exponent before negation.

Edge Cases

  • A nonzero base with n = 0 returns 1; zero to zero and zero to a negative exponent are excluded by the contract.
  • A zero base with positive n returns zero. Negative bases produce a negative answer only for odd exponent magnitude.
  • INT_MIN = -2147483648 requires positive magnitude 2147483648, which does not fit Java int; JavaScript Number and Python int represent it exactly.
  • Bases 1 and -1 remain unit magnitude for every exponent; parity determines the sign for -1.
  • Very large positive or negative exponents can underflow toward zero. Compare floating-point outputs using a tolerance rather than exact equality.

Common Mistakes and Interview Tips

  • Negating the Java int before widening overflows at INT_MIN; assign to long first.
  • Using JavaScript signed bitwise shifts on magnitude 2147483648 can make it negative; use Math.floor(exp / 2).
  • Forgetting the reciprocal for a negative exponent computes the opposite power.
  • Changing base before consuming an odd factor includes the wrong squared power in ans.
  • Interview tip: state the invariant, explain the odd/even cases, and distinguish the exact-arithmetic proof from floating-point rounding.
  • The repeated multiplication baseline needs billions of operations on general full-range inputs; use binary exponentiation for the complete constraints.

Key Takeaway

Halving the exponent while squaring the base converts linear repeated work into logarithmic work, the same idea behind fast modular exponentiation.