Data Structures & AlgorithmsVery hard
Why does the sum-of-suffix-maxima total equal a simple right-to-left scan?
- A. Suffix maxima are the same as the global maximum.
- B. The total is always the array length times the maximum.
- C. Suffix maxima require sorting the array.
- 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.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- When counting windows whose sum is a perfect square, why is the square test exact only with integer square roots?
- In the 'longest subarray with a single removal' interview problem, why is subtracting the minimum from the total a common mistake?
- Which property lets the 'longest subarray where every adjacent pair differs by exactly 1' be found in one pass?
- When computing the minimum adjacent swaps by breadth-first search over arrangements, why does the first time a target is reached give the…
- Intervals [(1, 3), (2, 5), (4, 6), (3, 4), (6, 8), (5, 7)] are closed integer ranges. Choose as many as possible so that every integer…
- Closed real intervals [(1, 4), (3, 6), (8, 9), (9, 12), (14, 15)] are merged whenever they overlap (sharing an endpoint counts as…