Data Structures & AlgorithmsVery hardCodeforces Div2 D · TCS NQT
What is the sum of the minimum element over every contiguous subarray of [3, 1, 2, 4, 1]? (Every subarray counts, including repeated values at different positions.)
- A. 47
- B. 22
- C. 11
- D. 17
Correct answer: B. 22
For each element count the subarrays where it is the minimum, using the nearest smaller element on each side (with tie-breaking on one side). Each element contributes value × left × right, so the total is a sum of such products.
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
- 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…
- 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…