Data Structures & AlgorithmsHard

How many index pairs i < j in [1, 5, 3, 3, 3, 7, 2, 4, 6] have DD[i] + DD[j] = 6? Equal values at different indices are different pairs.

  1. A. 3
  2. B. 10
  3. C. 5
  4. D. 8

Correct answer: C. 5

Pairs are counted by index, so three equal 3s give three pairs of 3 + 3 among positions. A set of value pairs collapses repeats, counting ordered pairs doubles the answer, and including i = j adds self-pairs.

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

Exams that ask this topic