Data Structures & AlgorithmsVery hardGoogle · Amazon coding round

How many pairs of indices (i, j) with i < j satisfy a[i] > 2·a[j] in a = [5, 2, 6, 1, 7, 3, 9, 2, 4]?

  1. A. 9
  2. B. 16
  3. C. 12
  4. D. 7

Correct answer: A. 9

Counting pairs with a[i] > 2·a[j] directly (or by a merge-sort-based count taken before merging) gives 9. The plain inversion count is 16, which ignores the factor 2.

More Coding Interview Problems questions

Exams that ask this topic