Data Structures & AlgorithmsHard

What is the length of the shortest contiguous block of 1 3 9 4 6 5 7 8 that, if sorted in place by itself, makes the WHOLE array sorted in non-decreasing order?

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

Correct answer: C. 6

The block must start at the first position where the array disagrees with its sorted copy and end at the last such position, so the length is the span between the first and last mismatch. Counting mismatches or adjacent descents counts different objects.

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

Exams that ask this topic