Data Structures & AlgorithmsModerateTCS NQT · Infosys

Array [4, 4, 7, 1, 7, 2]. How many index pairs i < j have equal values a[i] = a[j]?

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

Correct answer: B. 2

Each value that appears c times contributes C(c, 2) equal pairs. The value 4 appears twice and 7 appears twice, so there are 1 + 1 = 2 pairs; counting distinct values is a different quantity.

More Coding Interview Problems questions

Exams that ask this topic