Data Structures & AlgorithmsHardTCS NQT · Infosys pattern

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

  1. A. 265
  2. B. 90
  3. C. 103
  4. D. 89

Correct answer: B. 90

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 90. 265 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