Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current right-half element being merged in), how many inversions (pairs i < j with a[i] > a[j]) does [60, 21, 18, 29, 22, 34, 31, 50, 27, 49] have?
- A. 28
- B. 17
- C. 18
- D. 16
Correct answer: B. 17
Whenever an element from the right half is merged in before some remaining left-half elements, every one of those remaining left elements forms an inversion with it; summing these counts across the whole merge sort gives 17 inversions, matching the O(n²) brute-force pair count.
More Coding Interview Problems questions
- What is the length of the longest common subsequence of "ACEACCEBD" and "DBEADDEA"?
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- What is the length of the longest common subsequence of "AEEBDCCC" and "BEBEADE"?
- Problem: A string s (length n) must be searched for a pattern p (length m) in text. Which algorithm guarantees O(n+m) worst-case time…