Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

Swap the first two elements of [2, 4, 1, 3, 5], then count the inversions of the new array. What is the count?

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

Correct answer: C. 4

After the swap the array is [4, 2, 1, 3, 5]; its inversions are (4,2), (4,1), (4,3), (2,1), which total 4. Counting the original array's inversions gives a different number.

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

Exams that ask this topic