Data Structures & AlgorithmsModerateGATE CS · TCS NQT

Consider only the elements at even 0-based indices of [5, 2, 8, 1, 7, 3, 9, 4] (that is, 5, 8, 7, 9). How many inversions (pairs i < j of this subsequence with a[i] > a[j]) does it contain?

  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 8

Correct answer: A. 1

Extract the even-indexed subsequence first, then count inversions with merge sort or a BIT. Counting inversions over the whole array, instead of the chosen positions, gives a different count.

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

Exams that ask this topic