GATE Computer ScienceVery hardGATE CS (PYQ pattern)
int cnt = 0; int f(int n) { cnt++; if (n <= 1) return 1; if (n == 2) return f(1) + 1; return f(n - 1) + f(n - 2) + f(n - 3); } After f(6) returns, what are the return value and cnt?
- A. 24 and 30
- B. 24 and 32
- C. 21 and 32
- D. 27 and 34
Correct answer: B. 24 and 32
Let T(n) be the value and C(n) the number of calls: T(0) = T(1) = 1, T(2) = 2, then T(n) = T(n-1) + T(n-2) + T(n-3), giving T(6) = 24; C(n) = 1 + C(n-1) + C(n-2) + C(n-3) gives 32.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- int g(int n) { static int s = 0; s += n; if (n > 0) { g(n - 1); s += n; } return s; } int main() { int x = g(3); int y = g(2); printf("%d…
- void p(int n) { if (n == 0) return; printf("%d ", n); p(n - 1); printf("%d ", n 10); if (n % 2 == 0 && n > 2) p(n - 2); } How many numbers…
- void f(int x, int y, int z) { x = y + z; y = x - z; z = x; } int main() { int a = 1, b = 2, c = 3; f(&a, &b, c); f(&b, &a, c); f(&a, &a…
- int h(int n) { return n == 0 ? 0 : 2 h(n / 10) + n % 10; } What is h(2593)?
- int A(int m, int n) { if (m == 0) return n + 1; if (n == 0) return A(m - 1, 1); return A(m - 1, A(m, n - 1)); } How many calls to A…
- int f(int n); int g(int n) { return n <= 0 ? 1 : f(n - 1) + g(n - 2); } int f(int n) { return n <= 0 ? 0 : g(n - 1) + n; } What does f(6)…