Data Structures & AlgorithmsHardGATE CS · Codeforces Div2 C

What is the minimum number of perfect squares (1, 4, 9, 16, ...) whose sum is exactly 12? Repeats are allowed.

  1. A. 4
  2. B. 2
  3. C. 3
  4. D. 12

Correct answer: C. 3

12 = 9 + 1 + 1 + 1 needs 4 squares, but 4 + 4 + 4 needs only 3. Taking the largest square each time is the greedy trap; the correct method is the DP over all squares not exceeding the target.

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

Exams that ask this topic