Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional

Sorted positive array a = [1, 2, 3, 4, 6, 8]. How many pairs of indices i < j have a[i] * a[j] <= 12?

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

Correct answer: A. 9

With sorted positive values the valid partners of each i form a shrinking prefix, so two pointers count 5 + 3 + 1 = 9 pairs (1 with everything, 2 with 3, 4, 6, and 3 with 4). Using < 12 drops (2, 6) and (3, 4) (7), letting j start at i adds 1·1, 2·2, 3·3 (12), and counting ordered pairs gives 18.

More Coding Interview Problems questions

Exams that ask this topic