GATE Computer ScienceHardGATE CS (PYQ pattern)

The maximum of every window of 3 consecutive elements of [4, 2, 12, 11, -5, 7, 9, 3, 1, 6] is found with a deque of indices: before pushing index i, pop from the back while a[back] <= a[i]; push i; pop from the front if it has left the window. How many deque operations (back pops + pushes + front pops) are performed, and what is the sum of the window maxima?

  1. A. 17 operations, sum 80
  2. B. 19 operations, sum 80
  3. C. 19 operations, sum 73
  4. D. 22 operations, sum 82

Correct answer: B. 19 operations, sum 80

Window maxima are [12, 12, 12, 11, 9, 9, 9, 6] (sum 80). There are 10 pushes plus the pops from back and front, 19 operations in all.

More Programming and Data Structures (GATE) questions

Exams that ask this topic