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

Exams that ask this topic