Data Structures & AlgorithmsVery hard

For the 'ramp width' problem (max j − i with A[i] ≤ A[j]), why are only prefix minima and suffix maxima worth considering?

  1. A. Only the global maximum can be a right end.
  2. B. Ramps always start at index zero.
  3. C. Prefix minima and suffix maxima are the same for every array.
  4. D. A left end that is not a prefix minimum is beaten by an earlier, smaller value that gives a wider ramp, and a right end not a suffix maximum is beaten by a later, larger value.

Correct answer: D. A left end that is not a prefix minimum is beaten by an earlier, smaller value that gives a wider ramp, and a right end not a suffix maximum is beaten by a later, larger value.

An earlier, smaller left candidate dominates a later one, so only decreasing prefix minima matter; a later, larger right candidate dominates an earlier one, so only suffix maxima matter. Two sorted lists then give a linear algorithm.

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

Exams that ask this topic