GATE Computer ScienceHardGATE CSE · ISRO

int n = 200, cnt = 0; for (int i = 1; i <= n; i *= 2) for (int j = i; j <= n; j += i) cnt++; What is the value of cnt after the loops finish?

  1. A. 405
  2. B. 391
  3. C. 200
  4. D. 397

Correct answer: D. 397

Outer i takes 1,2,4,…,128; the inner loop runs ⌊200/i⌋ times: 200+100+50+25+12+6+3+1 = 397.

More Programming and Data Structures (GATE) questions

Exams that ask this topic