Data Structures & AlgorithmsHardICPC regional · Codeforces Div2 C

Problem: What is the minimum number of swaps of two neighbouring characters needed to turn "abcdef" into "fedcba"?

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

Correct answer: B. 15

Each adjacent swap fixes exactly one inversion, and the full reversal has C(6, 2) = 15 inversions. Counting ordered pairs gives 30, swapping arbitrary positions would need only 3, and 5 is just n − 1.

More Coding Interview Problems questions

Exams that ask this topic