Data Structures & AlgorithmsModerateCodeforces Div2 B · fast power
Problem: What is the complexity of binary exponentiation for computing a^b mod m?
- A. O(log b)
- B. O(b)
- C. O(sqrt b)
- 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
- Problem: What property of an array makes the two-pointer technique for pair sums valid?
- Problem: What is the purpose of a 'segment tree beats' structure?
- Problem: What is the number of comparisons needed in the worst case to check whether a sorted array of n elements contains a given value…
- Problem: What is the greedy choice used in the 'minimum number of arrows to burst balloons' problem?
- Problem: Which problem is solved by the Gale-Shapley algorithm in O(n^2) time with preference lists?
- Problem: Which data structure supports adding a value to a prefix of an array and finding the first index whose value is at least x, each…