What is the minimum number of successive swaps of adjacent letters in the string that are needed to change the string to (For example, swaps are required to change to one such sequence of swaps is )
- A)
- B)
- C)
- D)
- E)
Answer
D
Key insight
Each adjacent swap reverses the order of exactly one pair of letters, and all C(6,2) = 15 pairs must be reversed, so 15 swaps are necessary and sufficient.
Solution
Look at pairs of letters. In every pair is in alphabetical order; in every pair is reversed. There are pairs.
A swap of two adjacent letters changes the relative order of exactly one pair (the two letters swapped) and leaves every other pair alone. So each swap fixes at most one of the pairs, and at least swaps are needed.
Fifteen swaps suffice: move to the right end with swaps (), then to the second-to-last spot with swaps (), then with , with , with : total , ending at .
The answer is .
Why this works
The number of out-of-order pairs (inversions) changes by exactly per adjacent swap, so it is a monovariant that gives a lower bound, and the greedy "bubble" construction shows the bound is attained. Reversing letters always takes swaps; the given example with swaps is the small case of this pattern.
The trap
Guessing 6 (one swap per letter) or 24 from a factorial instead of counting the pairs whose order must change.
Common mistakes
- Guessing 6 (one swap per letter) or 24 from a factorial instead of counting the pairs whose order must change.
- Finding a -swap sequence but not arguing why fewer swaps cannot work.
Techniques
Use an invariant, parity, or coloring argument · Compute small cases, spot the pattern, generalize