Data Structures & AlgorithmsModerate

Add up the LENGTHS of all contiguous subarrays of 1 3 2 2 5 4 6 that are non-decreasing (each qualifying subarray contributes its length once).

  1. A. 12
  2. B. 18
  3. C. 11
  4. D. 84

Correct answer: B. 18

Each non-decreasing block contributes its length to the total, not one, so sum the lengths directly. Counting the blocks, dropping singletons, or summing lengths of all blocks each give different answers.

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

Exams that ask this topic