GATE Computer ScienceVery hardGATE CS (PYQ pattern)

int w(int n) { if (n < 0) return 0; if (n == 0) return 1; return w(n - 1) + w(n - 3) + w(n - 4); } What does w(10) return, and how many calls to w (including the first, and including calls with n < 0) does it make?

  1. A. 64, 313 calls
  2. B. 64, 168 calls
  3. C. 69, 313 calls
  4. D. 64, 469 calls

Correct answer: A. 64, 313 calls

Values: w(0..10) = 1, 1, 1, 2, 4, 6, 9, 15, 25, 40, 64, so w(10) = 64. Calls: C(n) = 1 for n <= 0 and C(n) = 1 + C(n-1) + C(n-3) + C(n-4) otherwise, giving C(1..10) = 4, 7, 10, 16, 28, 46, 73, 118, 193, 313. Leaving out the calls with n < 0 gives 168.

More Programming and Data Structures (GATE) questions

Exams that ask this topic