Data Structures & AlgorithmsVery hardICPC regional · Product Company SDE

Problem: The array [2, 3, 4, 5, 6, 7, 8] is examined for the number of subsets whose sum is a perfect square (the empty subset has sum 0, which counts as a perfect square; subsets are counted by their chosen positions).

  1. A. 16
  2. B. 15
  3. C. 5
  4. D. 128

Correct answer: A. 16

Brute force over the 128 subsets finds 16 with square sums (0, 4, 9, 16 or 25), including the empty one. Leaving it out gives 15, counting distinct square sums gives 5, and 128 counts all subsets.

More Coding Interview Problems questions

Exams that ask this topic