Data Structures & AlgorithmsVery hardGATE CS · DSA

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

  1. A. 67
  2. B. 78
  3. C. 68
  4. D. 161

Correct answer: C. 68

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 36 subarrays) gives 68. 161 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