870. Calculate the Power of a Number using Binary Exponentiation

You are given two integers x and n. Your task is to calculate xn using Binary Exponentiation, and return the result modulo 10⁹ + 7.

Example 1:

Input: x = 2, n = 5

Output: 32

Explanation: 25 = 2 * 2 * 2 * 2 * 2 = 32

Example 2:

Input: x = 21, n = 2

Output: 441

Explanation: 212 = 21 * 21 = 441

Now Your Turn!

Pick the correct output for the given input

Input: x = 5, n = 0

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  • 1 <= x <= 109
  • 0 <= n <= 109

Fun Facts

0
class Solution {
public:
int power(long long x, long long n) {
// Your code goes here
}
};
 
Test Case

Input:

N
X