Implement power function using binary exponentiation#269
Closed
aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
Closed
Implement power function using binary exponentiation#269aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
aryaman0406 wants to merge 1 commit intoSjxSubham:mainfrom
Conversation
Reviewer's guide (collapsed on small PRs)Reviewer's GuideThis PR adds a recursive binary exponentiation implementation (myPow and binaryExp) in C++, handling negative exponents and the INT_MIN edge case by casting the exponent to a long, and achieves O(log n) time complexity. File-Level Changes
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
Owner
|
this solution is already exixts |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR Title Format: 50.Pow(x, n).cpp💡 IntuitionThe naive approach of multiplying$x$ by itself $n$ times would be $O(n)$ , which is too slow for large values of $n$ . We can leverage the mathematical property that $x^n = x^{n/2} \cdot x^{n/2}$ . This allows us to reduce the number of multiplications significantly by halving the exponent in each step. This approach is $O(\log n)$ . Special care must be taken to handle negative exponents and the edge case where the exponent $n$ is the minimum integer value (INT_MIN).✍️ ApproachThe solution uses a recursive helper function, binaryExp(x, n), which handles the exponentiation:Base Case: If the exponent $n$ is $0$ , return $1$ (since $x^0 = 1$ ).Negative Exponent Handling: If $n$ is negative, we use the property $x^{-n} = 1 / x^n$ . The initial function myPow casts $n$ to a long to prevent overflow when negating the INT_MIN value, as -INT_MIN is not representable in a standard int.Odd Exponent: If $n$ is odd, we have $x^n = x \cdot x^{n-1}$ . Since $n-1$ is even, we can rewrite this as $x \cdot (x^2)^{(n-1)/2}$ . We multiply by $x$ once, square the base $x$ to get $x^2$ , and halve the new exponent to $(n-1)/2$ .Even Exponent: If $n$ is even, we have $x^n = (x^2)^{n/2}$ . We square the base $x$ to get $x^2$ and halve the exponent to $n/2$ .Efficiency: The total number of recursive calls (multiplications) is proportional to $\log n$ , resulting in an $O(\log n)$ time complexity.Code Solution (C++)C++class Solution {
public:
/**
* @brief Computes x raised to the power n using Binary Exponentiation.
* * @param x The base.
* @param n The exponent (integer).
* @return double The result x^n.
*/
double myPow(double x, int n) {
// Cast n to long to safely handle the INT_MIN case, where -n would overflow int.
return binaryExp(x, static_cast(n));
}
private:
double binaryExp(double x, long n) {
// Base case: x^0 = 1
if (n == 0) {
return 1.0;
}
};
Related IssuesBy submitting this PR, I confirm that:[x] This is my original work not totally AI generated[x] I have tested the solution thoroughly on leetcode[x] I have maintained proper PR description format[x] This is a meaningful contribution, not spam
Summary by Sourcery
New Features: