Data Structures & AlgorithmsHard

How many index pairs i < j in [2, 8, 3, 18, 4, 12, 5] have a product A[i]·A[j] that is a perfect square?

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

Correct answer: D. 4

A product is a perfect square exactly when every prime appears to an even power across the two factors, so test the product itself. Including i = j adds self-pairs, a divisibility shortcut is only a necessary condition, and testing the sum tests the wrong expression.

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

Exams that ask this topic