Skip to content

Commit 21165ed

Browse files
authored
Merge pull request #900 from debangi29/powx-n
Added solution for leetcode 50- Pow(x,n)!
2 parents d39a024 + 6d24d87 commit 21165ed

File tree

1 file changed

+139
-0
lines changed

1 file changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
---
2+
id: powx-n
3+
title: Pow(x,n)
4+
difficulty: Medium
5+
sidebar_label: 0050-powxn
6+
tags:
7+
- Math
8+
- Recursion
9+
---
10+
11+
## Problem Description
12+
13+
| Problem Statement | Solution Link | LeetCode Profile |
14+
| :---------------- | :------------ | :--------------- |
15+
| [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/) |
16+
17+
## Problem Description
18+
19+
Implement pow(x, n), which calculates x raised to the power n (i.e., $x^n$).
20+
21+
22+
23+
### Examples
24+
25+
### Example 1:
26+
27+
**Input**: x = 2.00000, n = 10
28+
29+
**Output**: 1024.00000
30+
31+
32+
### Example 2:
33+
34+
**Input**: x = 2.10000, n = 3
35+
36+
**Output**: 9.26100
37+
38+
### Example 3:
39+
40+
**Input**: x = 2.00000, n = -2
41+
42+
**Output**: 0.25000
43+
44+
**Explanation**: 2-2 = 1/22 = 1/4 = 0.25
45+
46+
47+
### Constraints
48+
49+
- $-100.0 < x < 100.0$
50+
- $-2^{31} \leq n \leq 2^{31} - 1$
51+
- $n$ is an integer.
52+
- Either $x$ is not zero or $n > 0$.
53+
- $-10^{4} \leq x^{n} \leq 10^{4}$
54+
55+
### Approach
56+
Initialize ans as 1.0 and store a duplicate copy of n i.e nn using to avoid overflow
57+
58+
Check if nn is a negative number, in that case, make it a positive number.
59+
60+
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.
61+
62+
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.
63+
64+
### Solution Code
65+
66+
#### Python
67+
68+
```
69+
class Solution:
70+
def myPow(x: float, n: int) -> float:
71+
ans = 1.0
72+
nn = n
73+
if nn < 0:
74+
nn = -1 * nn
75+
while nn:
76+
if nn % 2:
77+
ans = ans * x
78+
nn = nn - 1
79+
else:
80+
x = x * x
81+
nn = nn // 2
82+
if n < 0:
83+
ans = 1.0 / ans
84+
return ans
85+
```
86+
87+
#### Java
88+
89+
```
90+
class Solution {
91+
public static double myPow(double x, int n) {
92+
double ans = 1.0;
93+
long nn = n;
94+
if (nn < 0) nn = -1 * nn;
95+
while (nn > 0) {
96+
if (nn % 2 == 1) {
97+
ans = ans * x;
98+
nn = nn - 1;
99+
} else {
100+
x = x * x;
101+
nn = nn / 2;
102+
}
103+
}
104+
if (n < 0) ans = (double)(1.0) / (double)(ans);
105+
return ans;
106+
}
107+
}
108+
```
109+
110+
#### C++
111+
112+
```
113+
class Solution {
114+
public:
115+
double myPow(double x, int n) {
116+
double ans = 1.0;
117+
long long nn = n;
118+
if (nn < 0) nn = -1 * nn;
119+
while (nn) {
120+
if (nn % 2) {
121+
ans = ans * x;
122+
nn = nn - 1;
123+
} else {
124+
x = x * x;
125+
nn = nn / 2;
126+
}
127+
}
128+
if (n < 0) ans = (double)(1.0) / (double)(ans);
129+
return ans;
130+
}
131+
};
132+
133+
```
134+
135+
### Conclusion
136+
137+
- Time Complexity: $O(log n)$ , where n is the exponent; Using Binary exponentiation.
138+
139+
- Space Complexity: $O(1)$ as we are not using any extra space.

0 commit comments

Comments
 (0)