Data Structures & AlgorithmsHardHCLTech · Wipro pattern

What is the sum of the minimum element over every contiguous subarray of [7, 6, 6, 4, 8, 9]?

  1. A. 106
  2. B. 110
  3. C. 156
  4. D. 126

Correct answer: B. 110

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 110. 156 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