![]() |
VOOZH | about |
Given a positive integer n, find the minimum number of perfect squares that sum up to n. We can use each square any number of times.
Note: A number can always be represented as a sum of squares of other numbers. Because 1 is a square number and we can always break any number as (12 + 12 + 12 + ... ).
Examples :
Input: n = 100
Output: 1
Explanation: 100 can be written as [102] or [52 + 52 + 52 + 52] and the minimum number of perfect squares needed for 100 is 1.Input: n = 6
Output: 3
Explanation: Only possible way to make sum equals to 6 is [12 + 12 + 22], so minimum square numbers needed is 3.
Table of Content
In this approach, we solve the problem recursively by exploring all possible ways to form the number using perfect squares. For a given number n, we try subtracting every possible perfect square that is less than or equal to n.
For each subtraction, we recursively compute the minimum number of squares required to represent the remaining value. Among all these possibilities, we take the minimum result.
3
Time Complexity: O(nn), for every n we make sqrt(n) function calls.
Auxiliary Space: O(n) - recursive stack space
In this approach, we notice that many subproblems overlap because multiple larger subproblems depend on and call the same smaller subproblems, that lead to the same computation being done again and again. In order to avoid this redundant computation, we can store the results of subproblems in a dp array and reuse them whenever needed.
3
Time Complexity: O(n*sqrt(n)), as we compute each subproblem only once using memoization.
Auxiliary Space: O(n)
In this approach, we are iteratively calculating the answers beginning with the smallest subproblems β base cases. Using these base values, we then find the answers for larger numbers one by one. For each number, we check for all smaller subproblems(obtained by subtracting a perfect square from current number) to find the minimum count.
3
Time Complexity: O(n*sqrt(n))
Auxiliary Space: O(n), used for dp[] array.
In this approach, we iteratively find the minimum steps required to reach smaller values by subtracting perfect squares at each step, solving any subproblem we havenβt solved yet until we reach 0.
This can be visualized as a graph where the vertices are numbered from 0 to n, and there is an edge from u to v if (uβv) is a perfect square. Using Breadth-First Search (BFS) on this graph, we can efficiently find the minimum number of edges (steps) needed to reach vertex 0 starting from vertex n.
3
Time Complexity: O(n*sqrt(n))
Auxiliary Space: O(n), used for queue and visited array.
Lagrange's four-square theorem, also known as Bachet's conjecture, states that every non-negative integer can be represented as a sum of four non-negative integer squares. That is, the squares form an additive basis of order four:
p = a2 + b2 + c2 + d2, where the four numbers a, b, c, d are integers.
Therefore, we can deduce that any positive number can be expressed as sum of at-most 4 square numbers.
Case 1:
If number is a perfect square => minimum number of squares needed = 1
Example : 1, 4, 9, etc.
Case 2:
If the number is the sum of 2 square numbers => minimum number of squares needed = 2
Example : 2, 5, 18, etc.
Case 3:
If the number is not of the form 4k x (8m + 7) such that k, m β W => minimum number of squares needed = 3
Example : 6, 11, 12 etc.
Case 4:
If the number is of the form 4k x (8m + 7) such that k, m β W=> minimum number of squares needed = 4
Example : 7, 15, 23 etc.
3
Time Complexity: O(sqrt(n)) - to check if n is sum of 2 squared numbers, we run a for loop from 1 to sqrt(n).
Auxiliary Space: O(1)