GATE Computer ScienceHardGATE CS (PYQ pattern)

void h(int n, char a, char b, char c) { if (n == 0) return; h(n - 1, a, c, b); printf("move disk %d\n", n); h(n - 1, c, b, a); } During h(7, 'A', 'B', 'C'), which disk does the 50th printed line name, and how many of the first 49 lines name disk 1?

  1. A. disk 2; 25 lines
  2. B. disk 3; 25 lines
  3. C. disk 2; 24 lines
  4. D. disk 1; 25 lines

Correct answer: A. disk 2; 25 lines

The k-th printed move is disk 1 + (number of trailing zero bits of k), the ruler sequence of binary counting. 50 = 110010 in binary has one trailing zero, so line 50 names disk 2. Disk 1 moves on every odd-numbered line, and lines 1 to 49 contain 25 odd numbers.

More Programming and Data Structures (GATE) questions

Exams that ask this topic