ProgrammingVery hardGATE CS · Campus placements
A recursive Tower of Hanoi solution moves n disks, one at a time, with the rule that a larger disk never sits on a smaller one. How many moves does the recursion make for n = 10?
- A. 1000
- B. 1023
- C. 1024
- D. 512
Correct answer: B. 1023
The recurrence is M(n) = 2M(n-1) + 1 with M(0) = 0, which gives 2^n - 1. For n = 10 that is 1023. A common slip is to forget the single move of the largest disk.
More C Programming questions
- The Ackermann function is defined as A(0,n) = n+1, A(m,0) = A(m-1,1), and A(m,n) = A(m-1, A(m,n-1)) for m, n > 0. What is A(2,3)?
- int a[5] = {10, 20, 30, 40, 50}; int p = a + 1, q = a + 4; printf("%td %d", q - p, (p + 2)); prints what?
- void f(int a[10]) { printf("%zu", sizeof a); } Which statement about the result is correct?
- int m[2][3] = {{1, 2, 3}, {4, 5, 6}}; int (p)[3] = m; printf("%d", ((p + 1) + 2)); prints what?
- Which statement about sizeof is correct when x is an int variable initialised to 5?
- char s[] = "hello"; printf("%zu %zu", sizeof s, strlen(s)); prints what?