-
-
Notifications
You must be signed in to change notification settings - Fork 157
Added solution for leetcode 50- Pow(x,n)! #900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
--- | ||
id: powx-n | ||
title: Pow(x,n) | ||
difficulty: Medium | ||
sidebar_label: 0050-powxn | ||
tags: | ||
- Math | ||
- Recursion | ||
--- | ||
|
||
## Problem Description | ||
|
||
| Problem Statement | Solution Link | LeetCode Profile | | ||
| :---------------- | :------------ | :--------------- | | ||
| [Pow(x,n)](https://leetcode.com/problems/powx-n/description/) | [Pow(x,n) Solution on LeetCode](https://leetcode.com/problems/powx-n/solutions/) | [Leetcode Profile](https://leetcode.com/u/debangi_29/) | | ||
|
||
## Problem Description | ||
|
||
Implement pow(x, n), which calculates x raised to the power n (i.e., x<sup>n</sup>). | ||
|
||
|
||
|
||
### Examples | ||
|
||
### 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/22 = 1/4 = 0.25 | ||
|
||
|
||
### Constraints | ||
|
||
- -100.0 < x < 100.0 | ||
ajay-dhangar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
- -2<sup>31</sup> <= n <= 2<sup>31</sup>-1 | ||
- n is an integer. | ||
- Either x is not zero or n > 0. | ||
- -10<sup>4</sup> <= x<sup>n</sup> <= 10<sup>4</sup> | ||
|
||
### Approach | ||
Initialize ans as 1.0 and store a duplicate copy of n i.e nn using to avoid overflow | ||
|
||
Check if nn is a negative number, in that case, make it a positive number. | ||
|
||
Keep on iterating until nn is greater than zero, now if nn is an odd power then multiply x with ans ans reduce nn by 1. Else multiply x with itself and divide nn by two. | ||
|
||
Now after the entire binary exponentiation is complete and nn becomes zero, check if n is a negative value we know the answer will be 1 by and. | ||
|
||
### Solution Code | ||
|
||
#### Python | ||
|
||
``` | ||
class Solution: | ||
def myPow(x: float, n: int) -> float: | ||
ans = 1.0 | ||
nn = n | ||
if nn < 0: | ||
nn = -1 * nn | ||
while nn: | ||
if nn % 2: | ||
ans = ans * x | ||
nn = nn - 1 | ||
else: | ||
x = x * x | ||
nn = nn // 2 | ||
if n < 0: | ||
ans = 1.0 / ans | ||
return ans | ||
``` | ||
|
||
#### Java | ||
|
||
``` | ||
class Solution { | ||
public static double myPow(double x, int n) { | ||
double ans = 1.0; | ||
long nn = n; | ||
if (nn < 0) nn = -1 * nn; | ||
while (nn > 0) { | ||
if (nn % 2 == 1) { | ||
ans = ans * x; | ||
nn = nn - 1; | ||
} else { | ||
x = x * x; | ||
nn = nn / 2; | ||
} | ||
} | ||
if (n < 0) ans = (double)(1.0) / (double)(ans); | ||
return ans; | ||
} | ||
} | ||
``` | ||
|
||
#### C++ | ||
|
||
``` | ||
class Solution { | ||
public: | ||
double myPow(double x, int n) { | ||
double ans = 1.0; | ||
long long nn = n; | ||
if (nn < 0) nn = -1 * nn; | ||
while (nn) { | ||
if (nn % 2) { | ||
ans = ans * x; | ||
nn = nn - 1; | ||
} else { | ||
x = x * x; | ||
nn = nn / 2; | ||
} | ||
} | ||
if (n < 0) ans = (double)(1.0) / (double)(ans); | ||
return ans; | ||
} | ||
}; | ||
|
||
``` | ||
|
||
### Conclusion | ||
|
||
- Time Complexity: $O(log n)$ , where n is the exponent; Using Binary exponentiation. | ||
|
||
- Space Complexity: $O(1)$ as we are not using any extra space. |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.