Pow(x, n) - Complete Solution Guide
Pow(x, n) is LeetCode problem 50, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.
Problem Statement
Implement pow(x, n) , which calculates x raised to the power n (i.e., x n ). Example 1: Input: x = 2.00000, n = 10 Output: 1024.00000 Example 2: Input: x = 2.10000, n = 3 Output: 9.26100 Example 3: Input: x = 2.00000, n = -2 Output: 0.25000 Explanation: 2 -2 = 1/2 2 = 1/4 = 0.25 Constraints: -100.0 < x < 100.0 -2 31 <= n <= 2 31 -1 n is an integer. Either x is not zero or n > 0 . -10 4 <= x n <= 10 4
Detailed Explanation
The problem asks us to implement the `pow(x, n)` function, which calculates `x` raised to the power `n` (x^n). We are given a base `x` (a floating-point number) and an exponent `n` (an integer). The result should be a floating-point number. Constraints include bounds on x, n, and x^n, as well as handling the case where x is 0.
Solution Approach
The solution uses a binary exponentiation approach to calculate x^n efficiently. It iteratively squares the base x and halves the exponent n. If the current exponent n is odd, the current result is multiplied by the current value of x. This process continues until the exponent becomes 0.
Step-by-Step Algorithm
- Step 1: Handle the case where n is negative. If n < 0, replace x with 1/x and n with -n.
- Step 2: Initialize the result variable `res` to 1.0.
- Step 3: Iterate while n > 0. In each iteration:
- Step 4: Check if n is odd using the bitwise AND operator (n & 1). If it is, multiply the `res` by the current value of x.
- Step 5: Square the value of x (x = x * x).
- Step 6: Divide n by 2 using the bitwise right shift operator (n >>= 1).
- Step 7: After the loop finishes, return the value of `res`.
Key Insights
- Insight 1: Directly multiplying x by itself n times would result in O(n) time complexity. The key is to use the property that x^n = (x^(n/2))^2 if n is even, and x^n = x * (x^(n/2))^2 if n is odd. This allows us to reduce the number of multiplications significantly.
- Insight 2: We can use bit manipulation to efficiently check if n is odd (n & 1) and divide n by 2 (n >> 1). This avoids using modulo and division operators, which can be slower.
- Insight 3: Negative exponents can be handled by taking the reciprocal of the base (x) and negating the exponent (n). It's also important to consider integer overflow for the negative value of the exponent if the exponent is the minimum integer value (-2^31).
- Insight 4: The iterative approach avoids recursion, leading to better performance and no call stack overhead.
Complexity Analysis
Time Complexity: O(log n)
Space Complexity: O(1)
Topics
This problem involves: Math, Recursion.
Companies
Asked at: Accenture, Adobe, Amazon, Apple, Arcesium, Bloomberg, ByteDance, Citadel, EPAM Systems, Goldman Sachs, Infosys, LinkedIn, Meta, Microsoft, Millennium, Oracle, Qualcomm, Salesforce, ServiceNow, TikTok, Uber, Walmart Labs, Wix, Yahoo, eBay, tcs.