ProgrammingHardGATE CS · Competitive coding (CP)
How many subsets of {1, 2, 3, 4, 5, 6} (the empty set included) have a sum divisible by 3? Compute with this program.
How many subsets of {1, 2, 3, 4, 5, 6} (the empty set included) have a sum divisible by 3? Compute with this program.
#include <stdio.h>
int main(void) {
int count = 0;
for (unsigned m = 0; m < 64; m++) {
unsigned s = 0;
for (int i = 0; i < 6; i++)
if (m >> i & 1u) s += (unsigned)i + 1;
if (s % 3 == 0) count++;
}
printf("%d", count);
}- A. 24
- B. 20
- C. 18
- D. 32
Correct answer: A. 24
Each bit of m selects one element of {1..6}, so the loop checks all 64 subsets. Of these, 24 have a sum divisible by 3, including the empty set.
More C Programming questions
- What is printed by the first two calls? c #include <stdio.h> unsigned next(void) { static unsigned s = 1; s = s 1103515245u + 12345u…
- What is printed? c #include <stdio.h> int main(void) { int total = 0; for (unsigned x = 1; x <= 100; x++) { unsigned y = x; while (y) {…
- What is printed? c #include <stdio.h> #include <string.h> int main(void) { const char names[] = {"ann", "bob", "cat", "dan", "eve"}; const…
- What is printed? Assume long long is 64 bits and that the product fits in a long long. c #include <stdio.h> int main(void) { long long p =…
- What is printed? c #include <stdio.h> int clz16(unsigned short x) { int n = 0; while (!(x & 0x8000u)) { x <<= 1; n++; } return n; } int…
- What is printed? Assume ASCII character encoding. c #include <stdio.h> int main(void) { int d = 'z' - 'a'; char e = 'A' + d 2; printf("%d…