Data Structures & AlgorithmsVery hardGATE CS · ICPC regional

What is the minimum number of swaps of two (not necessarily adjacent) elements needed to sort the permutation [4, 2, 5, 1, 3] (0-based indices shifted so the values are 0 to 4)?

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

Correct answer: A. 2

The minimum number of swaps equals n minus the number of cycles of the permutation. Counting inversions gives a different number, which is not the right measure for arbitrary swaps.

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

Exams that ask this topic