50. Pow(x, n)
Practice Pow(x, n) for free on NudgeCode: Medium, Array & Hash Table pattern. Write it in Python or JavaScript, run the tests in your browser, and ask for a hint when you are stuck.
Compute x raised to the power n, without using a built-in power function. n can be negative or as large as 2^31 in size, so multiplying x by itself n times is too slow; aim for O(log n). Answers within 1e-5 are accepted.
Example
x = 2.0, n = 10 -> 1024.0
x = 2.1, n = 3 -> 9.261
x = 2.0, n = -2 -> 0.25
Constraints
- -100.0 < x < 100.0
- -2^31 <= n <= 2^31 - 1
- x is not 0 when n is negative
- -10^4 <= the answer <= 10^4