ProgrammingVery hardProduct company OA

How many subsets does this code count?

How many subsets does this code count?
int a[] = {2, 3, 5, 7, 8}, c = 0;
for (int m = 0; m < 32; m++) {
    int s = 0;
    for (int i = 0; i < 5; i++) if (m >> i & 1) s += a[i];
    if (s == 10) c++;
}
printf("%d", c);
  1. A. 2
  2. B. 5
  3. C. 3
  4. D. 4

Correct answer: C. 3

Each m from 0 to 31 is a bitmask choosing a subset, and m >> i & 1 parses as (m >> i) & 1, testing element i. The subsets with sum 10 are {2, 8}, {3, 7} and {2, 3, 5}.

More C Programming questions

Exams that ask this topic