Data Structures & AlgorithmsVery hardGATE CS · DSA

What is the sum of the minimum element over every contiguous subarray of [8, 1, 5, 4, 4, 2]?

  1. A. 111
  2. B. 51
  3. C. 50
  4. D. 58

Correct answer: B. 51

Using a monotonic stack, each a[i] contributes a[i] × (elements between it and the previous smaller) × (elements between it and the next smaller-or-equal). Summing this (or the direct brute force over all 21 subarrays) gives 51. 111 is the sum of the *maximum* of each subarray instead, a different quantity.

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

Exams that ask this topic