Data Structures & AlgorithmsVery hardGATE CS · competitive programming
A window of length 3 slides over [1, 3, −1, −3, 5, 3, 6, 7]. What is the sum of the maximum of each window?
- A. −4
- B. 24
- C. 33
- D. 29
Correct answer: D. 29
The window maxima are 3, 3, 5, 5, 6, 7, summing to 29. A monotonic deque finds each maximum in O(1) amortised time. The sum of minima, −4, is the trap for a flipped comparison.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- Treat [5, −3, 5] as a circle. What is the maximum sum of a non-empty contiguous subarray, where the subarray may wrap around the end?
- Ten people stand in a circle numbered 1 to 10. Counting from person 1, every 3rd person is eliminated and removed, and counting continues…
- The same rule, with every 3rd person eliminated, applies to n = 41 people. Who survives (1-based)?
- How many permutations of 1..5 can be produced by pushing 1, 2, 3, 4, 5 in order onto a stack and popping at any moment?
- How many pairs i < j in [2, 7, 3, 5, 8, 4] have A[i] + A[j] divisible by 3?
- How many non-empty contiguous subarrays of [4, 5, 0, 2, 3, 1] have a sum divisible by 5?