Data Structures & AlgorithmsVery hardICPC regional · Codeforces Div2 B

Problem: How many pairs i < j in the array [2, 8, 9, 18, 4, 32, 50, 72] have a product a[i] × a[j] that is a perfect square?

  1. A. 24
  2. B. 16
  3. C. 32
  4. D. 1

Correct answer: B. 16

Split by square-free part: 2, 8, 18, 32, 50, 72 all equal 2·k² (C(6, 2) = 15 pairs) and 9, 4 are squares (1 pair), so 16. Letting j start at i adds the 8 self-products (24), counting ordered pairs gives 32, and requiring both factors to be squares gives 1.

More Coding Interview Problems questions

Exams that ask this topic