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);
}
  1. A. 5
  2. B. 7
  3. C. 6
  4. 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

Exams that ask this topic