We need to express a given number n as a sum of perfect square numbers (1, 4, 9, 16, 25, ...) and find the minimum number of terms in that sum. We can reuse the same perfect square as many times as we want.
For example, 12 can be expressed in several ways: 1+1+1+1+1+1+1+1+1+1+1+1 (twelve 1s), or 4+4+4 (three 4s), or 9+1+1+1 (four terms). The best is 4+4+4 using only 3 squares.
This is a "minimum coins" problem. The "coins" are perfect squares (1, 4, 9, 16, ...), the "amount" is n, and we want the fewest coins to make that amount. That connection to the Coin Change problem guides every approach below.
1 <= n <= 10^4. With n up to 10,000, an O(n sqrt(n)) solution runs in about 10,000 100 = 1,000,000 operations, well within limits.For a given number n, try subtracting every possible perfect square and recursively solve the smaller subproblem. If we subtract j*j from n, we need 1 + numSquares(n - j*j) squares. Try all valid perfect squares and take the minimum.
The base case is numSquares(0) = 0 (zero needs zero squares). For any positive n, we iterate through 1, 4, 9, 16, ... up to n and pick the choice that gives the smallest count.
n == 0, return 0.result to n (worst case: using all 1s gives exactly n terms).j from 1 while j * j <= n, recursively compute numSquares(n - j * j) and update result = min(result, 1 + numSquares(n - j * j)).result.The recursion recomputes the same subproblems many times. The next approach builds solutions bottom-up and stores each result so nothing is recomputed.
Since the recursive solution has overlapping subproblems, we can use dynamic programming. We build a table dp where dp[i] is the minimum number of perfect squares that sum to i, filling it from dp[0] up to dp[n].
For each value i, we try every perfect square j*j that fits (where j*j <= i) and check: dp[i] = min(dp[i], dp[i - j*j] + 1). This is the Coin Change DP, where the "coins" are perfect squares.
Every dp[i] reads only values dp[i - j*j] where i - j*j < i. Since we fill the table in increasing order of i, each of those smaller values is finalized before dp[i] uses it. The inner loop treats each perfect square as a possible "last term" of the sum and keeps the choice that leads to the fewest total terms.
dp of size n + 1, initialized to n + 1 (a safe upper bound).dp[0] = 0 (base case: zero needs zero squares).i from 1 to n, for each j from 1 while j * j <= i: dp[i] = min(dp[i], dp[i - j * j] + 1).dp[n].The DP solution always builds the full table, even when the answer is 1 or 2. Modeling the problem as a shortest path lets the search stop as soon as it reaches 0.
Build a graph where each node is a number from 0 to n, and from any node i there is an edge to i - j*j for every perfect square j*j <= i. Every edge represents using one perfect square, so the minimum number of squares summing to n equals the length of the shortest path from n to 0. Since all edges have equal weight, BFS finds that shortest path.
Because BFS explores level by level, it stops the moment it reaches 0. If the answer is 1 or 2, it finishes after exploring few nodes.
n. Create a visited array to avoid reprocessing.depth = 0.depth, process all nodes at the current level, and for each node subtract every possible j*j. If we reach 0, return depth. Otherwise enqueue unvisited results.Both DP and BFS are O(n * sqrt(n)). Two results from number theory reduce the answer to a handful of constant-time checks.
Lagrange's Four-Square Theorem states that every positive integer is a sum of at most four perfect squares, so the answer is always 1, 2, 3, or 4. Legendre's Three-Square Theorem identifies exactly when four are required: when n has the form 4^a * (8b + 7). These two results turn the problem into a short sequence of checks. Test for 1 (is n a perfect square?), then for 4 (Legendre's form), then for 2 (does some n - j*j equal a square?). If none of those hold, the answer must be 3, since Lagrange caps it at 4 and Legendre has already ruled 4 out.
n is a perfect square. If yes, return 1.n is divisible by 4, divide by 4. If the result mod 8 equals 7, return 4.j from 1 while j*j <= n, check if n - j*j is a perfect square. If any pair works, return 2.