From ee4968d66188ffd24dffffe66b46d9275e08e6a4 Mon Sep 17 00:00:00 2001 From: debangi29 Date: Mon, 10 Jun 2024 03:06:20 +0530 Subject: [PATCH 1/4] Added solution for leetcode 50- Pow(x,n)! --- .../lc-solutions/0000-0099/0050-pow(x,n).md | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md diff --git a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md new file mode 100644 index 000000000..d87a9f8ee --- /dev/null +++ b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md @@ -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^n). + + + +### 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 +- -2^31 <= n <= 2^31-1 +- n is an integer. +- Either x is not zero or n > 0. +- -10^4 <= x^n <= 10^4 + +### 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. \ No newline at end of file From 6d60d598520a363a2bcbfdb45916403d9dce197b Mon Sep 17 00:00:00 2001 From: Debangi Ghosh <117537653+debangi29@users.noreply.github.com> Date: Mon, 10 Jun 2024 10:14:32 +0530 Subject: [PATCH 2/4] Updated 0050-pow(x,n).md --- dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md index d87a9f8ee..5f0fa8dab 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md +++ b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md @@ -16,7 +16,7 @@ tags: ## Problem Description -Implement pow(x, n), which calculates x raised to the power n (i.e., x^n). +Implement pow(x, n), which calculates x raised to the power n (i.e., xn). @@ -47,10 +47,10 @@ Implement pow(x, n), which calculates x raised to the power n (i.e., x^n). ### Constraints - -100.0 < x < 100.0 -- -2^31 <= n <= 2^31-1 +- -231 <= n <= 231-1 - n is an integer. - Either x is not zero or n > 0. -- -10^4 <= x^n <= 10^4 +- -104 <= xn <= 104 ### Approach Initialize ans as 1.0 and store a duplicate copy of n i.e nn using to avoid overflow @@ -136,4 +136,4 @@ class Solution { - 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. \ No newline at end of file +- Space Complexity: $O(1)$ as we are not using any extra space. From 81ad4b990541070e566b9454785b2cd98242b5f9 Mon Sep 17 00:00:00 2001 From: Debangi Ghosh <117537653+debangi29@users.noreply.github.com> Date: Mon, 10 Jun 2024 12:27:18 +0530 Subject: [PATCH 3/4] Updates 0050-pow(x,n).md --- dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md index 5f0fa8dab..b82c52b88 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md +++ b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md @@ -46,11 +46,11 @@ Implement pow(x, n), which calculates x raised to the power n (i.e., xn31 <= n <= 231-1 -- n is an integer. -- Either x is not zero or n > 0. -- -104 <= xn <= 104 +- $-100.0 < x < 100.0$ +- $-2^{31} \leq n \leq 2^{31} - 1$ +- $n$ is an integer. +- Either $x$ is not zero or $n > 0$. +- $-10^{4} \leq x^{n} \leq 10^{4}$ ### Approach Initialize ans as 1.0 and store a duplicate copy of n i.e nn using to avoid overflow From 6d24d87ed673f1d18fb0a7f4df9bdc84bba4411b Mon Sep 17 00:00:00 2001 From: Ajay Dhangar <99037494+Ajay-Dhangar@users.noreply.github.com> Date: Mon, 10 Jun 2024 13:00:12 +0530 Subject: [PATCH 4/4] Update 0050-pow(x,n).md --- dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md index b82c52b88..b752e9ca0 100644 --- a/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md +++ b/dsa-solutions/lc-solutions/0000-0099/0050-pow(x,n).md @@ -16,7 +16,7 @@ tags: ## Problem Description -Implement pow(x, n), which calculates x raised to the power n (i.e., xn). +Implement pow(x, n), which calculates x raised to the power n (i.e., $x^n$).