Data Structures & AlgorithmsModerateGATE CS · Codeforces Div2 D

How many pairs of positions (i < j) in [5, 2, 7, 2, 9, 1, 6] have a[i] < a[j]?

  1. A. 11
  2. B. 3
  3. C. 19
  4. D. 10

Correct answer: D. 10

Count with a merge sort or a BIT over compressed values: each element adds the number of earlier values that are smaller. Checking only adjacent pairs gives the count of consecutive rises, not all pairs.

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

Exams that ask this topic