Data Structures & AlgorithmsVery hard

Using only swaps of ADJACENT elements, what is the minimum number of swaps needed to make the MINIMUM value of 3 1 5 2 4 the FIRST element and the MAXIMUM value the LAST element?

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

Correct answer: C. 3

Moving the minimum to the front costs its index in swaps, moving the maximum to the back costs its distance to the end, and the two moves interact when the maximum lies before the minimum. A breadth-first search over arrangements confirms the exact cost.

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

Exams that ask this topic