Pow(x, n) (Binary Exponentiation)

Medium Binary Search Search on Answer Range Original on LeetCode

Pow(x, n) (Binary Exponentiation) is a medium binary search problem solved with the search on answer range pattern. The best approach, optimal (binary exponentiation), runs in O(log n) time and O(1) space. Below are 2 approaches in Java, from brute force up.

Problem

Implement pow(x, n), which computes x raised to the integer power n. n can be negative.

Examples

Example 1

Input
x = 3.0, n = 5
Output
243.0

Example 2

Input
x = 2.0, n = -3
Output
0.125

Constraints

  • -100 < x < 100
  • -2^31 <= n <= 2^31 - 1

Animated walkthrough

A narrated, step-by-step animation that builds the solution from the idea up. Press play, or step through it at your own pace.

Concepts first, then the problem and every approach, step by step.

Space to play or pause · ← → to jump a step · click or drag the bar to seek

Solutions

Try it yourself first. Then compare: each approach lists its idea, the steps, its time and space, and the Java code.

ApproachTimeSpace
Brute forceO(|n|)O(1)
Optimal (binary exponentiation)O(log n)O(1)

1Brute force

TimeO(|n|)Up to about 2 · 10^9 multiplications, too slow in practice.
SpaceO(1)

Multiply x by itself |n| times, and take the reciprocal if n is negative.

  1. result *= x, |n| times.
Java
class Solution {
    public double myPow(double x, int n) {
        long e = Math.abs((long) n);
        double r = 1;
        for (long i = 0; i < e; i++) r *= x;
        return n < 0 ? 1 / r : r;
    }
}

2Optimal (binary exponentiation)

TimeO(log n)
SpaceO(1)

Look at the exponent in binary. Keep squaring the base; whenever the current bit of the exponent is 1, multiply the result by the base. log n steps.

  1. e = |n| as a long; if n < 0, x = 1 / x.
  2. While e > 0: if e is odd, r *= x; x *= x; e >>= 1.
Java
class Solution {
    public double myPow(double x, int n) {
        long e = n;
        if (e < 0) { x = 1 / x; e = -e; }
        double r = 1;
        while (e > 0) {
            if ((e & 1) == 1) r *= x;
            x *= x;
            e >>= 1;
        }
        return r;
    }
}

Edge cases to test

  • n = 0
  • n = Integer.MIN_VALUE (negating overflows int; use long)
  • Negative n

Hints

Hint 1

x^n = (x^2)^(n/2), with one extra x when n is odd.

FAQ

What is the best time complexity for Pow(x, n) (Binary Exponentiation)?

Optimal (binary exponentiation) runs in O(log n) time and O(1) extra space.

Which pattern does Pow(x, n) (Binary Exponentiation) use?

It is a binary search problem that uses the search on answer range pattern. Other problems with the same pattern: Find Nth root of M, Sqrt(x), Find position of an element in a sorted array of infinite numbers (Galloping Search).

Is there a brute force solution for Pow(x, n) (Binary Exponentiation)?

Yes. Brute force takes O(|n|) time and O(1) space. Multiply x by itself |n| times, and take the reciprocal if n is negative.

Which edge cases should I test for Pow(x, n) (Binary Exponentiation)?

n = 0; n = Integer.MINVALUE (negating overflows int; use long); Negative n.