ProgrammingVery hardCompetitive coding (CP)
What is printed? This program tries every subset of {3, 5, 6} and prints the largest XOR of a subset (the empty subset counts as 0).
What is printed? This program tries every subset of {3, 5, 6} and prints the largest XOR of a subset (the empty subset counts as 0).
#include <stdio.h>
int main(void) {
int v[] = {3, 5, 6}, best = 0;
for (unsigned m = 0; m < 8; m++) {
int x = 0;
for (int i = 0; i < 3; i++)
if (m >> i & 1u) x ^= v[i];
if (x > best) best = x;
}
printf("%d", best);
}- A. 5
- B. 7
- C. 6
- D. 3
Correct answer: C. 6
The subset XORs are 0, 3, 5, 6, 6, 5, 3 and 0, so the largest is 6. Note that 3 ^ 5 ^ 6 = 0, which is why 6 is reached twice.
More C Programming questions
- What is printed? c #include <stdio.h> char g[3][4] = {"110", "010", "011"}; int fill(int r, int c) { if (r < 0 || c < 0 || r >= 3 || c >=…
- What is printed? c #include <stdio.h> #include <stdlib.h> int cmp(const void a, const void b) { const int x = a, y = b; int sx = x[0] +…
- What is printed? c #include <stdio.h> #include <stdlib.h> struct Item { int id; char tag; }; int cmp(const void k, const void e) { const…
- What is printed? c #include <stdio.h> int digitsum(unsigned long long n) { return n == 0 ? 0 : (int)(n % 10) + digitsum(n / 10); } int…
- What is printed? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int main(void) { uint8t x = 3; for (int i = 0; i < 5…
- What is printed? c #include <stdio.h> int main(void) { printf("%d", 07 + 0x10); }