Data Structures & AlgorithmsModerateCodeforces Div2 B · fast power

Problem: What is the complexity of binary exponentiation for computing a^b mod m?

  1. A. O(log b)
  2. B. O(b)
  3. C. O(sqrt b)
  4. D. O(b log m)

Correct answer: A. O(log b)

The exponent is halved each step, so about log2(b) multiplications suffice.

More Coding Interview Problems questions

Exams that ask this topic