Data Structures & AlgorithmsHardCodeforces Div2 C · TCS NQT
Sum the maximum of every contiguous window of length 3 in the array [1, 3, -1, -3, 5, 3, 6, 7] (there are n − k + 1 windows). What is the total?
- A. -4
- B. 7
- C. 29
- D. 26
Correct answer: C. 29
A monotonic deque holds indices with decreasing values inside the current window, so the front is always the window maximum in O(n). Summing the minima instead of the maxima, or reading only the final window, gives a different total.
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
- The stock span on day i is the number of consecutive days up to and including day i with price ≤ the price on day i. Prices are [100, 80…
- Bars of heights [4, 2, 0, 3, 2, 5, 1, 0, 3] (width 1 each) hold rainwater between them. How many units of water are trapped in total?
- Buildings of heights [5, 3, 8, 3, 2, 7, 9, 4] stand in a row viewed from the left. A building is visible if it is strictly taller than…
- The binary matrix [[1, 0, 1, 1, 0], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]] is given. What is the area of the largest rectangle…
- Using the same kind of binary matrix, [[1, 0, 1, 0, 0], [1, 0, 1, 1, 1], [1, 1, 1, 1, 1], [1, 0, 0, 1, 0]], what is the area of the…
- How many permutations of {1, 2, 3, 4, 5} can be sorted into increasing order by a single stack, when the input is read left to right and…