![]() |
VOOZH | about |
Given two integers x and y, compute xy efficiently. The approach should run in O(log y) time complexity and use O(1) auxiliary space.
Examples:
Input: x = 3, y = 19
Output: 1162261467
Explanation: 319 = 1162261467
Input: x = 2, y = 5
Output: 32
Explanation: 25 = 32
Instead of multiplying x repeatedly y times, we use the binary representation of y to make the computation faster. Every number can be written as the sum of powers of 2, so we only need to consider those powers where the binary bit is 1.
We traverse through all the bits of y from LSB to MSB.
1162261467