Data Structures & AlgorithmsHardICPC regional · Codeforces Div 2

P = [4, 1, 5, 2, 3] has 5 inversions. If its first element is moved to the end, how many inversions does the new array [1, 5, 2, 3, 4] have?

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

Correct answer: B. 3

Moving 4 from the front to the back removes the 3 inversions it had with the smaller values 1, 2, 3, and adds 1 inversion with the larger 5: 5 − 3 + 1 = 3.

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

Exams that ask this topic