GATE Computer ScienceHardGATE CS (PYQ pattern)

int p[10] = {1}; for (int r = 1; r <= 8; r++) for (int j = r; j >= 1; j--) p[j] += p[j - 1]; How many of p[0..8] are odd, and what is p[3] + p[5]?

  1. A. 2 odd, 112
  2. B. 3 odd, 112
  3. C. 2 odd, 126
  4. D. 1 odd, 104

Correct answer: A. 2 odd, 112

After 8 rounds the array holds row 8 of Pascal's triangle: [1, 8, 28, 56, 70, 56, 28, 8, 1]. Its odd entries number 2^(number of 1-bits of 8) = 2, and p[3] + p[5] = 56 + 56 = 112.

More Programming and Data Structures (GATE) questions

Exams that ask this topic