Data Structures & AlgorithmsVery hard

Why does the sum-of-suffix-maxima total equal a simple right-to-left scan?

  1. A. Suffix maxima are the same as the global maximum.
  2. B. The total is always the array length times the maximum.
  3. C. Suffix maxima require sorting the array.
  4. D. Each suffix maximum is the larger of the current element and the maximum of the suffix to its right, so a running maximum computed backwards gives every term.

Correct answer: D. Each suffix maximum is the larger of the current element and the maximum of the suffix to its right, so a running maximum computed backwards gives every term.

The suffix maximum at i depends only on the element at i and the suffix maximum at i+1, so scanning from the right with one variable produces all terms in linear time.

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

Exams that ask this topic