Data Structures & AlgorithmsHardIBM · Deloitte pattern

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

  1. A. 131
  2. B. 113
  3. C. 263
  4. D. 114

Correct answer: D. 114

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 114. 263 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