Data Structures & AlgorithmsHardGATE CS · competitive programming

How many inversions does A = [7, 5, 3, 8, 1, 6, 2, 4] have, i.e. pairs i < j with A[i] > A[j]?

  1. A. 18
  2. B. 4
  3. C. 28
  4. D. 17

Correct answer: A. 18

Counting every such pair gives 18. Counting only adjacent descents gives 4, and n(n−1)/2 = 28 is the count of all pairs, not of inversions.

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

Exams that ask this topic