ProgrammingHardCompetitive coding (CP)

How many pairs (i, j) with i < j have a[i] XOR a[j] equal to 5 for a = {1, 2, 3, 4}? Compute with this program.

How many pairs (i, j) with i < j have a[i] XOR a[j] equal to 5 for a = {1, 2, 3, 4}? Compute with this program.
#include <stdio.h>
int main(void) {
    int a[] = {1, 2, 3, 4}, c = 0;
    for (int i = 0; i < 4; i++)
        for (int j = i + 1; j < 4; j++)
            if ((a[i] ^ a[j]) == 5) c++;
    printf("%d", c);
}
  1. A. 2
  2. B. 1
  3. C. 0
  4. D. 3

Correct answer: B. 1

Only 1 XOR 4 = 5 among the pairs, since 1 ^ 2 = 3, 1 ^ 3 = 2, 2 ^ 3 = 1, 2 ^ 4 = 6, 3 ^ 4 = 7.

More C Programming questions

Exams that ask this topic