> For the complete documentation index, see [llms.txt](https://r24zeng.gitbook.io/leetcode-notebook/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://r24zeng.gitbook.io/leetcode-notebook/binary-search-and-tree/50.-pow-x-n.md).

# 50. Pow(x, n)

\# Medium

> 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;
