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);
}- A. 20 42
- B. 20 41
- C. 120 42
- 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
- What is printed? c int x = 3, y = 1; printf("%d %d", !x == y, !(x == y));
- For ints a and b with b != 0 and no overflow, which statement is always true in C99 and later?
- Why should a header file shared by several .c files not contain the line int counter = 0;?
- With 4-byte int, what is printed? c int n = 0; int f(void) { n++; return 1; } int main(void) { sizet s = sizeof(f()); printf("%zu %d", s…
- What is printed? c int n = 3, s = 0; while (n--) s += n; printf("%d %d", s, n);
- Assume ASCII. What is printed? c int cmp(const void a, const void b) { return strcmp((const char )a, (const char )b); } int main(void) {…