ProgrammingHardCompetitive coding (CP)

How many subsets of {1, 2, 3, 4, 5} sum to exactly 10? Compute with this program.

How many subsets of {1, 2, 3, 4, 5} sum to exactly 10? Compute with this program.
#include <stdio.h>
int main(void) {
    int c = 0;
    for (unsigned m = 0; m < 32; m++) {
        int s = 0;
        for (int i = 0; i < 5; i++)
            if (m >> i & 1u) s += i + 1;
        if (s == 10) c++;
    }
    printf("%d", c);
}
  1. A. 4
  2. B. 2
  3. C. 5
  4. D. 3

Correct answer: D. 3

The subsets with sum 10 are {1, 2, 3, 4}, {2, 3, 5} and {1, 4, 5}, so the count is 3.

More C Programming questions

Exams that ask this topic