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.)

  1. A. 47
  2. B. 22
  3. C. 11
  4. 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.

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

Exams that ask this topic