Data Structures & AlgorithmsHard

How many index pairs i < j in 0 1 2 2 3 4 0 satisfy A[i]·A[j] = A[i] + A[j]?

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

Correct answer: C. 2

The equation x·y = x + y rearranges to (x − 1)(y − 1) = 1, so the solutions are x = y = 0 or x = y = 2 (and pairs with 0). Counting index pairs with i ≤ j adds the diagonal, and a plain sum test checks a different equation.

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

Exams that ask this topic