Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B

Let A = [1, 4, 6, 9], B = [2, 3, 7] and S = [5, 9, 10, 13]. Count the ordered pairs (x from A, y from B) whose sum x + y lies in S.

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

Correct answer: C. 2

For each x in A, look up each y in B in a hash set S (or sort and use two pointers). Each pair is counted once because the first list supplies x and the second supplies y. Swapping roles counts a different set of ordered pairs.

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

Exams that ask this topic