ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    int a[] = {1, 3, -1, -3, 5, 3, 6, 7};
    int k = 3, dq[8], head = 0, tail = 0, total = 0;
    for (int i = 0; i < 8; i++) {
        while (tail > head && a[dq[tail - 1]] <= a[i]) tail--;
        dq[tail++] = i;
        if (dq[head] <= i - k) head++;
        if (i >= k - 1) total += a[dq[head]];
    }
    printf("%d", total);
}
  1. A. 27
  2. B. 31
  3. C. 33
  4. D. 29

Correct answer: D. 29

The deque holds the indices of the window maxima, which are 3, 3, 5, 5, 6 and 7 over the six windows. Their sum is 29.

More C Programming questions

Exams that ask this topic