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
All 15 pairs of letters must switch order, and one swap of neighbours switches only one pair, so at least 15 swaps are needed, and 15 are enough.
Solution
Start with the example in the problem. Going from to , the pairs and and all have to switch order: starts before and ends after it, and so on. That is pairs, and the example used swaps. This suggests counting pairs.
1. Count the pairs. In , every pair of letters is in alphabetical order. In , every pair is in the opposite order. So every pair must switch. With letters, the number of pairs is
( pairs with letters after it, with , and so on). This is also .
2. One swap flips only one pair. When you swap two letters that are next to each other, only those two letters change order. Every other letter stays on the same side of each of them. So each swap switches exactly one pair.
3. So at least 15 swaps are needed, because pairs have to switch and each swap handles only one.
4. 15 swaps are enough. Move to the end one step at a time ( swaps): . Then move right until it sits just before ( swaps): . Then ( swaps): . Then ( swaps): . Then ( swap): . Total: .
At least are needed and are enough, so the minimum is .
The answer is .
Why this works
When a move can only change a count by , that count tells you the fewest moves you need. Here the count is "how many pairs are still in the wrong order", and each swap of neighbours changes it by exactly . Reversing letters always takes swaps; the example with letters and swaps is the small case.
The trap
Guessing 6 (one swap per letter) or 24 instead of counting how many pairs of letters have to change order.
Concise 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 .
Review in the AoPS books
- Intermediate Counting & Probability, Chapter 13: Events With States (§13.4)
- Introduction to Counting & Probability, Chapter 1: Counting Is Arithmetic
- Prealgebra, Chapter 15: Problem-Solving Strategies (§15.1)
- Introduction to Counting & Probability, Chapter 2: Basic Counting Techniques
Techniques
Use an invariant, parity, or coloring argument · Compute small cases, spot the pattern, generalize