Data Structures & AlgorithmsVery hard

The 3-sum two-pointer method skips a value when it equals the previous value. Why?

  1. A. To make the algorithm run in O(1) space.
  2. B. To prevent counting the same triple of values more than once when the array contains repeated values.
  3. C. Because repeated values always have sum zero.
  4. D. Because the answer must be the number of distinct values.

Correct answer: B. To prevent counting the same triple of values more than once when the array contains repeated values.

After sorting, equal values produce identical triples, so skipping repeated first elements and pointer positions yields each distinct triple exactly once.

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

Exams that ask this topic