Data Structures & AlgorithmsHardGATE CS · TCS NQT

What is the length of the shortest contiguous subarray which, if sorted in place, makes the whole array [2, 6, 4, 8, 10, 9, 15] sorted?

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

Correct answer: B. 5

Compare the array with its sorted copy: the first and last mismatched positions bound the subarray. Here the mismatches run from index 1 to index 5, giving length 5.

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

Exams that ask this topic