Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the sum of the maximum over all contiguous subarrays of [3, 1, 2, 4]?

  1. A. 31
  2. B. 30
  3. C. 28
  4. D. 17

Correct answer: B. 30

Each element is the maximum of the subarrays in which it is the largest, counted by a left and right span with a monotonic stack. Summing directly over all ten subarrays gives the same total.

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

Exams that ask this topic