Data Structures & AlgorithmsVery hardICPC regional · Amazon SDE OA

How many contiguous subarrays of [2, 8, 3, 2, 6, 1] have a product that is a perfect square?

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

Correct answer: C. 6

Track the parity vector of prime exponents; equal prefix vectors bound a square product. The squares are [2,8], [3,2,6], [3,2,6,1], [2,8,3,2,6], [2,8,3,2,6,1] and the single [1], total 6. Forgetting the one-element [1] gives 5; 21 counts all subarrays.

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

Exams that ask this topic