ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
#include <stdlib.h>
int nd(int n) {
    int c = 0;
    for (int d = 1; d <= n; d++)
        if (n % d == 0) c++;
    return c;
}
int cmp(const void *a, const void *b) {
    int x = *(const int *)a, y = *(const int *)b;
    int dx = nd(x), dy = nd(y);
    if (dx != dy) return dx - dy;
    return x - y;
}
int main(void) {
    int v[] = {12, 7, 9, 8};
    qsort(v, 4, sizeof v[0], cmp);
    for (int i = 0; i < 4; i++) printf("%d ", v[i]);
}
  1. A. 7 9 8 12
  2. B. 7 8 9 12
  3. C. 12 8 9 7
  4. D. 9 7 8 12

Correct answer: A. 7 9 8 12

Divisor counts are 6 for 12, 2 for 7, 3 for 9 and 4 for 8. Sorting by divisor count gives 7, 9, 8, 12.

More C Programming questions

Exams that ask this topic