Data Structures & AlgorithmsHard

How many contiguous subarrays of 3 1 2 4 0 contain EXACTLY 2 inversions (pairs i < j inside the subarray with a[i] > a[j])?

  1. A. 3
  2. B. 5
  3. C. 2
  4. D. 6

Correct answer: A. 3

An inversion lives entirely inside a subarray, so each block's inversion count is independent of the rest of the array. Counting at-least-two, the count with one inversion, or the whole array's inversions answers a different question.

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

Exams that ask this topic