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);- A. 2
- B. 5
- C. 3
- 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
- What is the type of the result of subtracting two int pointers, p - q?
- What is printed? c char s[] = "xya", p = s; char a = p++; char b = ++p; char c = ++p; printf("%c%c%c %s", a, b, c, s);
- What is printed? c int hs(unsigned n) { return n ? (n & 0xF) + hs(n >> 4) : 0; } int main(void) { printf("%d", hs(0xBEEF)); }
- How many swaps does this bubble sort make? c int a[] = {3, 1, 4, 1, 5}, sw = 0; for (int i = 0; i < 5; i++) for (int j = 0; j < 4 - i…
- What is printed? c printf("%d %d", strncmp("abc", "abd", 2) == 0, strncmp("ab", "ab0x", 5) == 0);
- uint8t a = 200, b = 100; uint8t c = a + b; What is the value of c?