ProgrammingHardTCS NQT · Infosys

For a = {2, 4, 2, 2, 6, 4}, how many index pairs i < j satisfy a[i] == a[j]?

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

Correct answer: A. 4

The value 2 occurs three times, giving 3 pairs, and 4 occurs twice, giving 1 pair, for a total of 4. Counting each value's c(c-1)/2 pairs after sorting runs in O(N log N), while the nested loop is too slow for N = 2*10^5.

More C Programming questions

Exams that ask this topic