---
description: '# Medium'
---

# 50. Pow(x, n)

> It's not like medium problem. The key is to consider two cases:&#x20;
>
> 1. n >= 0
> 2. n < 0
>
> Binary Search

{% tabs %}
{% tab title="Python" %}
```python
class Solution:
    def myPow(self, x: float, n: int) -> float:
        # edge case
        if abs(n) == 0:
            return 1
        
        # regular case
        if n&1 == 0:
            result = self.myPow(x, n//2)
            result = result*result
        else:
            if n > 0:
                result = self.myPow(x, n//2)
                result = result*result*x
            else:
                result = self.myPow(x, n//2+1)
                result = result*result/x

        return result
```
{% endtab %}
{% endtabs %}

Time complexity = $$O(lgN)$$&#x20;
