Data Structures & AlgorithmsVery hard

How many index quadruples i < j < k < l in 1 -1 2 -2 3 -3 have A[i] + A[j] + A[k] + A[l] = 0?

  1. A. 2
  2. B. 9
  3. C. 4
  4. D. 3

Correct answer: D. 3

Enumerate the four indices in increasing order and test the sum; equal-magnitude opposite pairs produce most of the zero sums. Counting triples, a target of one, or non-negative sums each change the total.

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

Exams that ask this topic