Data Structures & AlgorithmsHard

How many contiguous subarrays of length at least 2 of 3 1 4 2 5 contain an EVEN number of inversions (pairs i < j inside the subarray with a[i] > a[j])? Zero counts as even.

  1. A. 8
  2. B. 7
  3. C. 3
  4. D. 2

Correct answer: D. 2

Each block's inversions are counted inside the block only, and zero is even so ascending blocks qualify. Flipping the parity, including singletons, or counting only the sorted blocks each change the answer.

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

Exams that ask this topic