ProgrammingModerate

Which statement about the nested loop for (i = 0; i < n; i++) for (j = 1; j < n; j *= 2) is correct?

  1. A. It runs about n log2 n iterations in total
  2. B. It runs about n^2 iterations
  3. C. It runs about n iterations, since the inner loop is constant
  4. D. It runs about n^2 / 2 iterations

Correct answer: A. It runs about n log2 n iterations in total

The inner loop doubles j, so it has about log2 n steps for each of the n values of i.

More C Programming questions

Exams that ask this topic