Data Structures & AlgorithmsModerateICPC regional · Amazon SDE OA

Sort [6, 3, 4, 1, 2, 5] so that all even numbers come first in increasing order, then all odd numbers in increasing order, using adjacent swaps only. How many adjacent swaps are needed?

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

Correct answer: C. 7

Adjacent-swap cost equals the number of inversions with respect to the target order. Each pair out of target order costs exactly one adjacent swap, so counting inversions under the parity-then-value key gives the total.

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

Exams that ask this topic