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?
- A. 7
- B. 6
- C. 3
- 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.
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
- A circular linked list holds the values 2 7 4 9 1 6 3 in order, with the last node linked back to the first. How many nodes are STRICTLY…
- In 4 7 4 2 7 2 9, which value occurs most often? If several values tie, pick the one whose FIRST occurrence is earliest. Give that value.
- How many contiguous substrings of length exactly 4 in "abcadbcde" consist of four DISTINCT characters?
- Five people weigh 3 5 3 4 2. Each boat holds at most two people and its total weight must not exceed 7. What is the minimum number of…
- What is the smallest positive integer k such that the residues of 3 5 8 12 14 modulo k (that is, each A[i] mod k) are all DISTINCT?
- How many index triples i < j < k in "abcdeacace" spell the subsequence 'ace' (characters need not be adjacent)?