ProgrammingHardTCS NQT · Accenture

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

How many pairs (i, j) with i < j satisfy a[i] < a[j] for a = {3, 1, 4, 2}? Compute with this program.
#include <stdio.h>
int main(void) {
    int a[] = {3, 1, 4, 2}, c = 0;
    for (int i = 0; i < 4; i++)
        for (int j = i + 1; j < 4; 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

The increasing pairs are (3,4), (1,4) and (1,2), so the count is 3.

More C Programming questions

Exams that ask this topic