ProgrammingModerate

In a loop for (i = 0; i < n; i++) x = x * 2 % m; with m = 1000000007 and x kept as long long, what is the cost?

  1. A. O(n) multiplications and modulo operations, each constant time
  2. B. O(n^2), since the modulo costs n
  3. C. O(log n), since the value doubles
  4. D. O(2^n), since the value doubles each time

Correct answer: A. O(n) multiplications and modulo operations, each constant time

Each iteration does a constant amount of work, so the loop is linear in n. The value stays reduced modulo m.

More C Programming questions

Exams that ask this topic