Pow(x, n) (Binary Exponentiation)
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, Python, C++ and JavaScript, 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
Solve it here
Write your solution in Java, Python, C++ or JavaScript and run it against the examples, right in the page. Submit runs the hidden tests too and, with a free account, records the solve in your revision log.
Your own testcases: Run checks these, and the expected output comes from our reference solution.
My submissions
Nothing yet. Every Run and Submit you make while signed in shows up here.
| When | Result | Tests | Runtime | Your time | Type |
|---|
Run checks the testcases above (2 examples, or your own). Submit also runs 16 hidden tests and saves the solve to your log. Signed in, every Run and Submit is saved under My submissions; your draft stays in this browser.
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 code in Java, Python, C++ and JavaScript.
| Approach | Time | Space |
|---|---|---|
| Brute force | O(|n|) | O(1) |
| Optimal (binary exponentiation) | O(log n) | O(1) |
1Brute force
O(|n|)Up to about 2 · 10^9 multiplications, too slow in practice.O(1)Multiply x by itself |n| times, and take the reciprocal if n is negative.
- result *= x, |n| times.
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;
}
}class Solution:
def myPow(self, x: float, n: int) -> float:
e = abs(n)
r = 1.0
for _ in range(e):
r *= x
return 1 / r if n < 0 else rclass Solution {
public:
double myPow(double x, int n) {
long long e = llabs((long long)n);
double r = 1;
for (long long i = 0; i < e; i++) r *= x;
return n < 0 ? 1 / r : r;
}
};function myPow(x, n) {
const e = Math.abs(n);
let r = 1;
for (let i = 0; i < e; i++) r *= x;
return n < 0 ? 1 / r : r;
}2Optimal (binary exponentiation)
O(log n)O(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.
- e = |n| as a long; if n < 0, x = 1 / x.
- While e > 0: if e is odd, r *= x; x *= x; e >>= 1.
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;
}
}class Solution:
def myPow(self, x: float, n: int) -> float:
e = n
if e < 0:
x = 1 / x
e = -e
r = 1.0
while e > 0:
if e & 1 == 1:
r *= x
x *= x
e >>= 1
return rclass Solution {
public:
double myPow(double x, int n) {
long 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;
}
};function myPow(x, n) {
let e = n;
if (e < 0) { x = 1 / x; e = -e; }
let r = 1;
while (e > 0) {
if (e % 2 === 1) r *= x;
x *= x;
e = Math.floor(e / 2);
}
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.