Data Structures & AlgorithmsHard
In the 'shortest block to sort so the whole array becomes sorted' problem, why does the block start at the first mismatch with the sorted copy?
- A. The first element is always the smallest.
- B. Elements before the first mismatch already hold their final sorted values, so the sorted block must begin exactly there.
- C. Sorting always starts from index 0.
- D. The first mismatch is always the array maximum.
Correct answer: B. Elements before the first mismatch already hold their final sorted values, so the sorted block must begin exactly there.
Positions before the first mismatch are already in their final place, so any sorted block must include everything from that position onward. The same argument fixes the right end at the last mismatch.
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
- For an anagram search with a fixed window of length 3, why is a count comparison cheap per step?
- For the 'ramp width' problem (max j − i with A[i] ≤ A[j]), why are only prefix minima and suffix maxima worth considering?
- Why does pairing the heaviest remaining person with the lightest one (when they fit together) minimise boats when each boat carries at…
- The minimum number of consecutive deletions to make an array non-decreasing is found by which structure?
- Why is k ≥ n a necessary condition for the residues of n values modulo k to be distinct?
- When counting subsequences 'ace' in a string, why does a DP over the pattern prefixes avoid double counting?