GATE Computer ScienceEasyGATE CS (PYQ pattern)

for (i = 1; i < 1000; i *= 2) for (j = 0; j < i; j++) cnt++; What is the final value of cnt (starting from 0)?

  1. A. 1000
  2. B. 10000
  3. C. 1023
  4. D. 1535

Correct answer: C. 1023

The outer loop runs for i = 1, 2, 4, ..., 512 and the inner loop does i iterations each time: 1 + 2 + ... + 512 = 1023 (a geometric series, so O(n), not O(n log n)).

More Programming and Data Structures (GATE) questions

Exams that ask this topic