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++)
โก View Alternative Approaches with Code and Analysis
๐ 2๏ธโฃ Dynamic Programming Approach
๐ก Algorithm Steps:
Create a DP array where dp[i] represents minimum squares for number i.
Initialize dp[0] = 0 as base case.
For each number, try all perfect squares less than it.
Take minimum of all possible combinations to build the number.
๐ Complexity Analysis:
Time: โฑ๏ธ O(nโn) - Nested loops with square root boundary
Auxiliary Space: ๐พ O(n) - DP array storage
โ
Why This Approach?
Solves all subproblems systematically
Can be extended to find actual square numbers
Works for any positive integer
๐ 3๏ธโฃ BFS Level-Order Approach
๐ก Algorithm Steps:
Use BFS to explore all reachable numbers by subtracting perfect squares.
Each level represents using one more perfect square.
First time we reach 0 gives us the minimum count.
Track visited numbers to avoid redundant computations.
๐ Complexity Analysis:
Time: โฑ๏ธ O(nโn) - BFS exploration with square root iterations
Auxiliary Space: ๐พ O(n) - Queue and visited array
โ
Why This Approach?
Guarantees shortest path to solution
Intuitive level-based counting
Natural fit for finding minimum operations
๐ 4๏ธโฃ Mathematical Optimization
๐ก Algorithm Steps:
Check if n is perfect square - return 1.
Remove all factors of 4 from n.
If remaining number โก 7 (mod 8), return 4.
Otherwise check for sum of two squares, else return 3.
๐ Complexity Analysis:
Time: โฑ๏ธ O(โn) - Only checking up to square root
Auxiliary Space: ๐พ O(1) - Constant space usage
โ
Why This Approach?
Based on Lagrange's Four Square Theorem
Eliminates impossible cases early
Most efficient for single query
๐ ๐ Comparison of Approaches
๐ Approach
โฑ๏ธ Time Complexity
๐พ Space Complexity
โ Pros
โ ๏ธ Cons
๐ฏ Lagrange Theorem
๐ข O(โn)
๐ข O(1)
โก Fastest single query
๐งฎ Requires mathematical insight
๐ Dynamic Programming
๐ก O(nโn)
๐ก O(n)
๐ Reusable for multiple queries
๐ Slower for large n
๐ BFS Level-Order
๐ก O(nโn)
๐ก O(n)
๐ฏ Intuitive minimum path
๐พ High memory usage
๐งฎ Math Optimization
๐ข O(โn)
๐ข O(1)
๐ Optimal complexity
๐ง Complex theorem application
๐ Best Choice Recommendation
๐ฏ Scenario
๐๏ธ Recommended Approach
๐ฅ Performance Rating
๐ Single query optimization
๐ฅ Lagrange Theorem
โ โ โ โ โ
๐ Multiple queries
๐ฅ Dynamic Programming
โ โ โ โ โ
๐ Learning/Interview
๐ฅ BFS Approach
โ โ โ โ โ
๐ฏ Competitive Programming
๐ Math Optimization
โ โ โ โ โ
โ Code (Java)
๐ Code (Python)
๐ง 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