Data Structures & AlgorithmsVery hard

When an array's values are all distinct, what is the number of ordered pairs (i, j), i ≠ j, with A[i] > A[j]?

  1. A. n²
  2. B. n(n+2)
  3. C. n(n−1)/2
  4. D. n(n−1)

Correct answer: C. n(n−1)/2

Each unordered pair of distinct values contributes exactly one ordered pair with the larger value first, giving C(n,2) = n(n−1)/2. The other formulas count both orders or include the diagonal.

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

Exams that ask this topic