ProgrammingVery hardProduct company OA
What is printed?
What is printed?
int ways(int *a, int n, int t) {
if (t == 0) return 1;
if (n == 0 || t < 0) return 0;
return ways(a + 1, n - 1, t - a[0]) + ways(a + 1, n - 1, t);
}
int main(void) {
int a[] = {1, 2, 3, 4, 5};
printf("%d", ways(a, 5, 6));
}- A. 4
- B. 2
- C. 10
- D. 3
Correct answer: D. 3
Each call either takes a[0] or skips it, so ways counts subsets of {1, 2, 3, 4, 5} that add up to 6: {1, 5}, {2, 4} and {1, 2, 3}. Order does not matter because each element is considered once, in sequence.
More C Programming questions
- What value does this call to printf return? c printf("%5.1f|%-5d|", 3.14159, 42);
- What is printed? c const char w[] = {"red", "green", "blue", "cyan"}; const char p[] = {w + 2, w, w + 3, w + 1}; const char q = p…
- What is printed? c int m(int n) { return n > 100 ? n - 10 : m(m(n + 11)); } int main(void) { printf("%d %d", m(50), m(105)); }
- What is printed? c int h(unsigned a, unsigned b) { int c = 0; for (unsigned x = a ^ b; x; x >>= 1) c += x & 1; return c; } int main(void)…
- What is printed? c int a[] = {10, 20, 30, 40, 50}; int p = &a[3]; printf("%d %d %d", p[-2], -1[p], (p - 1)[-1]);
- What is printed? c struct C { int r, g, b; } c = {.b = 9, .r = 4}; printf("%d %d %d", c.r, c.g, c.b);