Data Structures & AlgorithmsHard

In 6 0 8 2 4 5 1 9, a ramp is a pair i < j with A[i] ≤ A[j]. What is the largest possible width j − i over all ramps?

  1. A. 7
  2. B. 6
  3. C. 3
  4. D. 1

Correct answer: A. 7

Prefix minima (6, 0) are the left candidates and suffix maxima (9) the right: 6 at index 0 ≤ 9 at index 7 gives width 7. Pairing the global minimum 0 (index 1) gives 6, the longest non-decreasing run (2, 4, 5) gives 3, and adjacent ramps only 1.

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

Exams that ask this topic