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);
}- A. 2
- B. 1
- C. 0
- 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
- What is printed? c #include <stdio.h> struct N { int v; struct N nx; }; struct N rev(struct N h, struct N prev) { if (!h) return prev…
- What is printed? c #include <stdio.h> int main(void) { unsigned long long r = 1, b = 7; int e = 222; while (e) { if (e & 1) r = r b %…
- What is printed? c #include <stdio.h> int main(void) { unsigned h = 0; for (const char s = "abcdefgh"; s; s++) h = (h << 5) ^ (unsigned…
- What is printed? c #include <stdio.h> int pc(int x) { int c = 0; while (x) { c += x & 1; x >>= 1; } return c; } int cmp(const void a…
- What is printed? c #include <stdio.h> int C(int n) { if (n == 0) return 1; int s = 0; for (int i = 0; i < n; i++) s += C(i) C(n - 1 - i)…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> int main(void) { unsigned x = (unsigned)-5; printf("%u", x >> 31); }