Data Structures & AlgorithmsModerateCodeforces Div2 C · TCS NQT

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

  1. A. 7
  2. B. 4
  3. C. 5
  4. D. 1

Correct answer: C. 5

Sort and fix the first element, then use two pointers on the rest (or a hash set), counting duplicates carefully. Counting only triples that use distinct values undercounts when values repeat.

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

Exams that ask this topic