ProgrammingHardCompetitive coding (CP)
What is printed?
What is printed?
#include <stdio.h>
int main(void) {
int a[] = {3, 1, 2, 5, 4}, c = 0;
for (int i = 0; i < 5; i++)
for (int j = i + 1; j < 5; j++)
if (a[i] > a[j]) c++;
printf("%d", c);
}- A. 2
- B. 4
- C. 3
- D. 5
Correct answer: C. 3
An inversion is a pair i < j with a[i] > a[j]. The pairs are (3,1), (3,2) and (5,4), so the count is 3.
More C Programming questions
- What is printed? c #include <stdint.h> #include <stdio.h> int main(void) { uint8t m = 0x0F; for (int i = 0; i < 8; i++) m = (uint8t)((m <<…
- What is printed? c #include <stdio.h> int main(void) { int x = 6; printf("%d", x & -x); }
- What is printed? c #include <stdio.h> int main(void) { const char s = " a bb ccc "; int words = 0, in = 0; for (; s; s++) { if (s == ' ')…
- What is printed? c #include <stdio.h> int ways(int n) { if (n == 0) return 1; if (n < 0) return 0; return ways(n - 1) + ways(n - 3); } int…
- What is printed? c #include <stdint.h> #include <stdio.h> uint64t pw(uint64t b, uint64t e, uint64t m) { uint64t r = 1; b %= m; while (e) {…
- What is printed? c #include <stdio.h> int main(void) { unsigned char comp[51] = {0}; int count = 0; for (int i = 2; i <= 50; i++) { if…