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);
}- A. 27
- B. 31
- C. 33
- 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
- How many values x in 0..255 satisfy (x & (uint8t)-x) == x? Assume uint8t is 8 bits. c #include <stdint.h> #include <stdio.h> int…
- What is printed? c #include <stdio.h> int f(int n) { static int calls = 0; calls++; if (n <= 0) return calls; int r = f(n - 2); return r…
- What is printed? c #include <stdio.h> struct P { int v; }; int main(void) { struct P arr[3] = {{1}, {2}, {3}}; int sum = 0; for (struct P…
- What is printed? c #include <stdio.h> int main(void) { char c = 'a'; printf("%d %d", c + 1 == 'b', '9' - '0'); }
- What is printed? c #include <stdio.h> unsigned fibcalls = 0; int fib(int n) { fibcalls++; if (n < 2) return n; return fib(n - 1) + fib(n…
- What is printed for the sorted array below? c #include <stdio.h> int main(void) { int a[] = {1, 3, 3, 3, 5, 8}; int n = 6, lo = 0, hi = n…