Data Structures & AlgorithmsHardGATE CS · TCS NQT

How many index pairs i < j in [1, 2, 3, 2, 1, 4] have a[i] + a[j] equal to 3 or to 6?

  1. A. 7
  2. B. {C+5}
  3. C. 9
  4. D. 6

Correct answer: D. 6

Pairs summing to 3 are 1 with 2, and pairs summing to 6 are 2 with 4 or 3 with 3 (requiring two 3s, which the array lacks). Hashing the complements counts each pair once.

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

Exams that ask this topic