Data Structures & AlgorithmsVery hardICPC regional · GATE CS

How many permutations of 1, 2, 3, 4, 5 have EXACTLY 4 inversions? An inversion is a pair of positions i < j with p[i] > p[j].

  1. A. 20
  2. B. 15
  3. C. 22
  4. D. 5

Correct answer: A. 20

The inversion-count distribution satisfies I(n, k) = I(n−1, k) + I(n−1, k−1) + … + I(n−1, k−n+1), and the total over all k is n!. Counting permutations with exactly k inversions needs this recurrence rather than k itself.

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

Exams that ask this topic