Data Structures & AlgorithmsHard

When the maximum and minimum must end at the two extremes by adjacent swaps, why can their costs interact?

  1. A. The costs are always independent of each other.
  2. B. Only the minimum ever needs to move.
  3. C. If the maximum lies before the minimum, moving the minimum to the front pushes the maximum by one position, so the two costs are not simply additive.
  4. D. Swaps cannot change the position of the maximum.

Correct answer: C. If the maximum lies before the minimum, moving the minimum to the front pushes the maximum by one position, so the two costs are not simply additive.

Moving the minimum leftward across the maximum shifts the maximum's index, so one of the moves saves a swap compared with the naive sum. Accounting for this interaction is the subtle part.

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

Exams that ask this topic