Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: How many pairs (i, j) with i < j in the array [4, 8, 1, 5, 9, 2, 7, 3, 6] satisfy a[j] − a[i] = 3?

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

Correct answer: D. 2

Scanning pairs with i < j and testing a[j] − a[i] = 3 counts each index pair once. Testing a[i] − a[j] or ignoring the index order double-counts or misses pairs.

More Coding Interview Problems questions

Exams that ask this topic