Data Structures & AlgorithmsModerateCodeforces Div2 B-D · ICPC regional

How many index triples i < j < k satisfy a[i] + a[j] + a[k] <= 4 for a = [-4, -1, 0, 2, 3, 5]?

  1. A. 14
  2. B. 11
  3. C. 16
  4. D. 15

Correct answer: A. 14

Sorting then fixing the smallest index and using two pointers counts all triples in O(n^2). Checking sums only up to 3 or 5 shifts the count by one or two triples.

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

Exams that ask this topic