ProgrammingVery hardProduct company OA

What is printed?

What is printed?
int c = 0, calls = 0;
void comb(int start, int n, int k) {
    calls++;
    if (k == 0) { c++; return; }
    for (int i = start; i <= n; i++) comb(i + 1, n, k - 1);
}
int main(void) {
    comb(1, 6, 3);
    printf("%d %d", c, calls);
}
  1. A. 20 42
  2. B. 20 41
  3. C. 120 42
  4. D. 20 20

Correct answer: A. 20 42

Choosing increasing indices gives each 3-element subset of 6 exactly once: C(6, 3) = 20. Calls count every node of the recursion tree: 1 root, 6 first choices, 15 pairs (some of which cannot be completed) and 20 complete triples, 42 in all. '120' counts ordered selections.

More C Programming questions

Exams that ask this topic