ProgrammingHardTCS NQT · Accenture
How many pairs (i, j) with i < j have a[i] == a[j]? Compute with this program.
How many pairs (i, j) with i < j have a[i] == a[j]? Compute with this program.
#include <stdio.h>
int main(void) {
int a[] = {1, 2, 3, 1, 2, 3}, c = 0;
for (int i = 0; i < 6; i++)
for (int j = i + 1; j < 6; j++)
if (a[i] == a[j]) c++;
printf("%d", c);
}- A. 2
- B. 4
- C. 3
- D. 6
Correct answer: C. 3
The equal pairs are (0,3), (1,4) and (2,5), so the count is 3. Each value occurs exactly twice, so no value gives more than one pair.
More C Programming questions
- What is printed? c #include <stdio.h> int main(void) { int x = -1; printf("%u", (unsigned)x >> 28); }
- What is printed? c #include <stdio.h> int ways(int n, int coins[], int k) { if (n == 0) return 1; if (n < 0 || k == 0) return 0; return…
- What is printed? Assume unsigned int is 32 bits. c #include <stdio.h> unsigned f(unsigned n) { unsigned r = 1; for (unsigned i = 2; i <=…
- What is printed? c #include <stdio.h> int main(void) { unsigned a[] = {5, 3}; printf("%u", a[0] - a[1] 2); }
- What is printed? c #include <stdio.h> int main(void) { int p = (int[]){1, 2, 3}; printf("%d", p[1] p[2]); }
- What is printed? c #include <stdio.h> int main(void) { printf("%u", 1u << 31 >> 30); }