Data Structures & AlgorithmsVery hard

Consider all cyclic rotations of 3 1 2 5 4 (including the identity). For each rotation count its inversions (pairs i < j with a[i] > a[j]). What is the MINIMUM inversion count over the rotations?

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

Correct answer: B. 3

Enumerate the n rotations and count inversions in each; the minimum is attained by the rotation that starts at the smallest block. Counting the original arrangement, the maximum, or an off-by-one count each give different values.

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

Exams that ask this topic