Data Structures & AlgorithmsVery hardCodeforces Div2 C · ICPC regional

Using only swaps of ADJACENT elements, what is the minimum number of swaps needed to turn [1, 2, 3, 2, 1, 3, 3] into a palindrome? (It is guaranteed that a palindrome arrangement exists.)

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

Correct answer: A. 4

Fix the outermost pair greedily: bring the matching partner of the leftmost element to the rightmost free position using adjacent swaps, then recurse on the inside. This greedy is optimal for palindrome arrangement with adjacent swaps, while swapping arbitrary pairs is not restricted by this rule.

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

Exams that ask this topic