05. Get Minimum Squares
โ GFG solution for Get Minimum Squares problem: find the minimum number of perfect squares that sum up to n using mathematical theorem and alternative DP approaches. ๐
The problem can be found at the following link: ๐ Question Link
๐งฉ Problem Description
Given a positive integer n, find the minimum number of perfect squares (square of an integer) that sum up to n.
Note: Every positive integer can be expressed as a sum of square numbers since 1 is a perfect square, and any number can be represented as 11 + 11 + 1*1 + ....
๐ Examples
Example 1
Input: n = 100
Output: 1
Explanation: 10 * 10 = 100Example 2
Input: n = 6
Output: 3
Explanation: 1 * 1 + 1 * 1 + 2 * 2 = 6๐ Constraints
$1 \le n \le 10^4$
โ
My Approach
The optimal approach uses Lagrange's Four Square Theorem combined with mathematical optimization to efficiently determine the minimum number of perfect squares needed:
Mathematical Optimization (Lagrange's Theorem)
Check for Perfect Square (Answer = 1):
Calculate the square root of n and verify if it's a perfect square.
If yes, return 1 immediately.
Check for Sum of Two Squares (Answer = 2):
Iterate through all perfect squares up to โn.
For each square iยฒ, check if (n - iยฒ) is also a perfect square.
If found, return 2.
Check for Answer = 4 (Based on Lagrange's Theorem):
Remove all factors of 4 from n by repeatedly dividing.
If the resulting number follows the form (8k + 7), return 4.
This is based on the mathematical property that numbers of form 4^a(8b + 7) require exactly 4 squares.
Default Answer = 3:
If none of the above conditions are met, return 3.
By Lagrange's theorem, every positive integer can be expressed as sum of at most 4 perfect squares.
๐ Time and Auxiliary Space Complexity
Expected Time Complexity: O(โn), as we iterate up to the square root of n to check for sum of two squares, and perform constant time operations for other checks.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for variables regardless of input size.
๐งโ๐ป Code (C++)
class Solution {
public:
int minSquares(int n) {
if(int s = sqrt(n); s * s == n) return 1;
for(int i = 1; i * i <= n; i++)
if(int r = sqrt(n - i * i); r * r == n - i * i) return 2;
while(n % 4 == 0) n /= 4;
return n % 8 == 7 ? 4 : 3;
}
};โ Code (Java)
class Solution {
public int minSquares(int n) {
int s = (int)Math.sqrt(n);
if(s * s == n) return 1;
for(int i = 1; i * i <= n; i++) {
int r = (int)Math.sqrt(n - i * i);
if(r * r == n - i * i) return 2;
}
while(n % 4 == 0) n /= 4;
return n % 8 == 7 ? 4 : 3;
}
}๐ Code (Python)
class Solution:
def minSquares(self, n):
s = int(n ** 0.5)
if s * s == n: return 1
for i in range(1, int(n ** 0.5) + 1):
r = int((n - i * i) ** 0.5)
if r * r == n - i * i: return 2
while n % 4 == 0: n //= 4
return 4 if n % 8 == 7 else 3๐ง Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: ๐ฌ Any Questions?. Let's make this learning journey more collaborative!
โญ If you find this helpful, please give this repository a star! โญ
๐Visitor Count
Last updated