Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 D

Problem: In the array [5, 1, 4, 2, 8, 0, 2], count the pairs (i, j) with i < j and a[i] > 2·a[j] (so a[i] is more than double a[j]).

  1. A. 11
  2. B. 12
  3. C. 9
  4. D. 4

Correct answer: C. 9

Brute force over i < j finds 9 pairs with a[i] > 2·a[j] (e.g. 5 > 2·1, 5 > 2·0, 4 > 2·0, 8 > 2·2). Using ≥ adds the equality cases for 11, plain inversions a[i] > a[j] give 12, and reversing the roles (2·a[i] < a[j]) gives 4.

More Coding Interview Problems questions

Exams that ask this topic