Data Structures & AlgorithmsModerateCodeforces Div2 C/D · Amazon SDE OA

Find the sum of the maximum of every window of length 3 in a = [1, 3, −1, −3, 5, 3, 6, 7] (windows slide one step at a time).

Find the sum of the maximum of every window of length 3 in a = [1, 3, −1, −3, 5, 3, 6, 7] (windows slide one step at a time).
a=[1,3,-1,-3,5,3,6,7]
print(sum(max(a[i:i+3]) for i in range(len(a)-2)))
What is printed?
  1. A. 22
  2. B. -4
  3. C. 26
  4. D. 29

Correct answer: D. 29

Each window contributes its maximum; a monotonic deque gives the same numbers in linear time. Window minima, windows of length four, and dropping the final window each change the sum.

More Coding Interview Problems questions

Exams that ask this topic