Data Structures & AlgorithmsVery hard

When summing the MAXIMUM over all subarrays with repeated values, the standard contribution method uses one strict and one non-strict comparison. Why?

  1. A. It is a speed trick and gives the same total with both sides strict.
  2. B. Because equal values are never the maximum of any subarray.
  3. C. To make the sum of the minimums and the sum of the maximums equal.
  4. D. So each subarray is credited to exactly one position of its maximum; otherwise equal maxima are counted twice or not at all.

Correct answer: D. So each subarray is credited to exactly one position of its maximum; otherwise equal maxima are counted twice or not at all.

With equal maxima, both positions would claim the same subarray unless one side breaks ties. Using a strict comparison on one side and a non-strict one on the other assigns every subarray to its leftmost or rightmost maximum exactly once.

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

Exams that ask this topic