ProgrammingVery hardProduct company OA · GATE CS

What is printed?

What is printed?
int cat(int n) {
    if (n <= 1) return 1;
    int s = 0;
    for (int i = 0; i < n; i++) s += cat(i) * cat(n - 1 - i);
    return s;
}
int main(void) { printf("%d %d", cat(4), cat(5)); }
  1. A. 16 42
  2. B. 14 42
  3. C. 14 32
  4. D. 10 30

Correct answer: B. 14 42

This is the Catalan recurrence C(n) = Σ C(i)·C(n − 1 − i): 1, 1, 2, 5, 14, 42. It counts, for example, the binary trees with n nodes. '32' would be 2^5.

More C Programming questions

Exams that ask this topic