Data Structures & AlgorithmsModerateCodeforces Div2 B-D · ICPC regional

What is the minimum number of swaps of ANY two (not necessarily adjacent) elements needed to sort the permutation [4, 3, 2, 1] into increasing order?

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

Correct answer: A. 2

Minimum swaps equals n minus the number of cycles. The cycles are (1 4) and (2 3), so 4 - 2 = 2. The number of inversions 6 counts adjacent swaps, which is a different and larger quantity.

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

Exams that ask this topic