Data Structures & AlgorithmsVery hard

Which property makes the number of inversions of a rotated sorted array equal to a simple product?

  1. A. All elements are equal.
  2. B. The array has no duplicate values.
  3. C. The array is already sorted.
  4. D. Every element of the first block is larger than every element of the second block, so each cross pair is an inversion and within-block pairs are ordered.

Correct answer: D. Every element of the first block is larger than every element of the second block, so each cross pair is an inversion and within-block pairs are ordered.

In a rotation of an ascending array, the first block is entirely larger than the second, so every cross pair is an inversion and each block is internally sorted. Counting the cross pairs gives the product of the block sizes.

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

Exams that ask this topic