ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int v[] = {1, 2, 3};
int cnt(int i, int s) {
    if (i == 3) return s % 2 == 0;
    return cnt(i + 1, s) + cnt(i + 1, s + v[i]);
}
int main(void) {
    printf("%d", cnt(0, 0));
}
  1. A. 3
  2. B. 5
  3. C. 4
  4. D. 2

Correct answer: C. 4

The subsets with an even sum are the empty set, {2}, {1, 3} and {1, 2, 3}, which makes 4.

More C Programming questions

Exams that ask this topic