Data Structures & AlgorithmsModerateTCS NQT Digital · Infosys

Problem: Find the number of pairs of indices (i, j), i < j, in the array [1, 3, 2, 4, 3, 5] such that a[i] = a[j] + 1 or a[i] = a[j] − 1.

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

Correct answer: B. 6

Pairs with |a[i] − a[j]| = 1 over all i < j number 6. Checking only adjacent positions gives 2, allowing equal values (|diff| ≤ 1) gives 7, and testing only a[j] = a[i] + 1 gives 4.

More Coding Interview Problems questions

Exams that ask this topic