Problem
You get a number x and an integer n. Return x^n, which is x to the power n.
For a positive n, x^n is the product of n copies of x. The value of x^0 is 1. For a negative n, x^n is 1 / x^(-n).
Do not use a built-in power function or operator. Find a solution that runs in O(log |n|) time, where |n| is the absolute value of n.
Example 1
- Input:
- x = 1.5, n = 3
- Output:
- 3.37500
- Explanation:
1.5 * 1.5 * 1.5is 3.375.
Example 2
- Input:
- x = 2.5, n = 2
- Output:
- 6.25000
Example 3
- Input:
- x = 0.5, n = -3
- Output:
- 8.00000
- Explanation:
- A negative power gives
1 / 0.5^3, which is1 / 0.125, or 8.
Constraints
- -100.0 < x < 100.0
- -2^31 <= n <= 2^31 - 1
- x is not 0, or n is larger than 0.
- -10^4 <= x^n <= 10^4
SysRound wrote this statement. A reference solution checks every example.
Related questions
Continue with questions that share topics or the same question type.
Sources · 1 reference
These records explain where the question appears. A company tag or candidate report does not guarantee a future interview question.
Hello Interview · 1 record
Meta
Leetcode 50. Pow(x, n)
Reported Apr 12, 2026 · 29 source reports
MANAGER, MID_LEVEL, SENIOR, STAFF
Source snapshot: Nov 12, 2026
View source