Data Structures & AlgorithmsHardCodeforces Div2 C · GATE CS

What is the sum of (max minus min) over all windows of size 3 in [2, 7, 1, 8, 2]?

  1. A. 21
  2. B. 24
  3. C. 17
  4. D. 20

Correct answer: D. 20

Each window of size 3 contributes its spread: windows [2,7,1] gives 6, [7,1,8] gives 7, [1,8,2] gives 7, totalling 20. Sliding deques give the same result in linear time.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic