Data Structures & AlgorithmsModerate

How many inversions (pairs i < j with A[i] > A[j]) does 4 5 6 1 2 3 contain?

  1. A. 8
  2. B. 11
  3. C. 9
  4. D. 1

Correct answer: C. 9

Every value of the first block (4, 5, 6) exceeds every value of the second block (1, 2, 3), so each cross pair is an inversion and there are 3 × 3 = 9 of them; the blocks are internally sorted. Adjacent descents count a different quantity.

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

Exams that ask this topic