ProgrammingHardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {1, 2, 3}, cnt[3], total = 0;
    for (int i = 0; i < 3; i++) {
        cnt[i] = 1;
        for (int j = 0; j < i; j++)
            if (a[j] < a[i]) cnt[i] += cnt[j];
        total += cnt[i];
    }
    printf("%d", total);
}
  1. A. 6
  2. B. 8
  3. C. 4
  4. D. 7

Correct answer: D. 7

cnt[i] counts the increasing subsequences that end at a[i], giving 1, 2 and 4. Their sum, 7, is the number of non-empty increasing subsequences.

More C Programming questions

Exams that ask this topic