Data Structures & AlgorithmsHardCodeforces Div 2 C · ICPC regional

Sorted array a = [-4, -1, 0, 2, 3, 5, 7]. How many triples of distinct indices i < j < k have a[i] + a[j] + a[k] <= 6?

  1. A. 18
  2. B. 24
  3. C. 42
  4. D. 21

Correct answer: D. 21

Fix i and move two pointers over the suffix: if the sum is at most T, every index between j and k also pairs with k, so the block adds k - j to the count. Using a strict inequality drops triples whose sum is exactly 6.

More Coding Interview Problems questions

Exams that ask this topic