Data Structures & AlgorithmsModerateTCS NQT · Codeforces Div2 B

How many index pairs (i < j) in [4, 9, 1, 7, 12, 5, 8, 3] satisfy a[i] − a[j] = 3 (exactly 3, in this order)?

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

Correct answer: C. 1

Use a frequency map and, for each j, look up a[j] + d among earlier values (the earlier element is larger by exactly d). Counting pairs with a[j] − a[i] = d, the reverse order, gives a different count.

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

Exams that ask this topic