Pow(x, n) is a mathematical function to calculate the value of x raised to the power of n (i.e., x^n).
Given a floating-point value x and an integer value n, implement the myPow(x, n) function, which calculates x raised to the power n.
You may not use any built-in library functions.
Example 1:
Input: x = 2.00000, n = 5
Output: 32.00000Example 2:
Input: x = 1.10000, n = 10
Output: 2.59374Example 3:
Input: x = 2.00000, n = -3
Output: 0.12500Constraints:
-100.0 < x < 100.0-1000 <= n <= 1000n is an integer.x = 0, then n will be positive.
You should aim for a solution as good or better than O(logn) time and O(logn) space, where n is the given integer.
A brute force approach would be to iterate linearly up to n, multiplying by x each time to find (x^n). If n is negative, return (1 / (x^n)); otherwise, return (x^n). Can you think of a better way? Maybe a recursive approach would be more efficient.
For example, to calculate 2^6, instead of multiplying 2 six times, we compute 2^3 and square the result. The same logic applies recursively to find 2^3 and further break down the multiplication. What should be the base case for this recursion? Maybe you should consider the term that cannot be further broken down.
In (x^n), if x is 0, we return 0. If n is 0, we return 1, as any number raised to the power of 0 is 1. Otherwise, we compute (x^(n/2)) recursively and square the result. If n is odd, we multiply the final result by x. What should be the logic if n is negative?
We start the recursion with the absolute value of n. After computing the result as res, we return res if n is non-negative; otherwise, we return (1 / res).
Before attempting this problem, you should be comfortable with:
We are asked to compute ( x^n ), where:
x is a floating-point numbern can be positive, zero, or negativeThe most straightforward way to think about exponentiation is:
x by itself n timesThis brute force approach directly follows the mathematical definition of power:
n is positive → multiply x repeatedlyn is zero → the result is always 1n is negative → compute ( x^{|n|} ) and take its reciprocalAlthough this method is not efficient for large n, it is very easy to understand and is a good starting point.
x == 0, return 0n == 0, return 1res = 1 to store the result.abs(n) times:res by xn is positive:resn is negative:1 / resComputing ( x^n ) by multiplying x repeatedly works, but it becomes very slow when n is large.
A much better idea is to use binary exponentiation, which is based on these observations:
n is even:n is odd:This means we can:
By doing this recursively, the number of multiplications reduces from O(n) to O(log n).
We also handle negative powers by:
n is negativehelper(x, n):x == 0, return 0n == 0, return 1res = helper(x * x, n // 2)n is odd:x * resn is even:reshelper(x, abs(n)) to compute the magnitude.n is negative:1 / resultresultWe want to compute ( x^n ) efficiently, even when n is very large (positive or negative).
The brute force approach multiplies x repeatedly and takes O(n) time, which is too slow.
Instead, we use binary exponentiation, which reduces the time complexity to O(log n).
The key ideas are:
n can be written in binarypower is odd, we must include one extra x in the resultx = x * x)power = power // 2)For negative powers:
abs(n) and take the reciprocal at the end if neededThis iterative version avoids recursion and works efficiently with constant extra space.
x == 0, return 0n == 0, return 1res = 1 (stores the final answer)power = abs(n) (work with positive exponent)power > 0:power is odd:res by xx = x * xpower = power >> 1n is negative:1 / resresWhen n is Integer.MIN_VALUE (-2147483648), computing abs(n) or -n overflows because the positive equivalent exceeds Integer.MAX_VALUE. Always cast to a long before taking the absolute value to avoid this undefined behavior.
A negative exponent means the result is 1 / x^|n|. Omitting this reciprocal step returns the wrong answer for all negative n values. Compute the power using the absolute value, then invert if n was negative.
Multiplying x by itself n times works but times out for large n (e.g., n = 2^31 - 1). Binary exponentiation reduces the number of operations from O(n) to O(log n) by squaring the base and halving the exponent at each step.