Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

Which technique counts the inversions of an array of n distinct integers in O(n log n) time?

  1. A. Insertion sort with a single comparison per element
  2. B. A simple nested loop only
  3. C. Merge sort, counting cross-inversions while merging (or a Fenwick tree)
  4. D. Hashing each element into buckets

Correct answer: C. Merge sort, counting cross-inversions while merging (or a Fenwick tree)

During the merge step, every element taken from the right half jumps over the remaining left-half elements, and each such jump counts as inversions.

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

Exams that ask this topic