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 = 100

Example 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)

  1. 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.

  2. 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.

  3. 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.

  4. 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;
    }
};
โšก View Alternative Approaches with Code and Analysis

๐Ÿ“Š 2๏ธโƒฃ Dynamic Programming Approach

๐Ÿ’ก Algorithm Steps:

  1. Create a DP array where dp[i] represents minimum squares for number i.

  2. Initialize dp[0] = 0 as base case.

  3. For each number, try all perfect squares less than it.

  4. Take minimum of all possible combinations to build the number.

class Solution {
public:
    int minSquares(int n) {
        vector<int> dp(n + 1, INT_MAX);
        dp[0] = 0;
        for(int i = 1; i <= n; i++)
            for(int j = 1; j * j <= i; j++)
                dp[i] = min(dp[i], dp[i - j * j] + 1);
        return dp[n];
    }
};

๐Ÿ“ 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:

  1. Use BFS to explore all reachable numbers by subtracting perfect squares.

  2. Each level represents using one more perfect square.

  3. First time we reach 0 gives us the minimum count.

  4. Track visited numbers to avoid redundant computations.

class Solution {
public:
    int minSquares(int n) {
        queue<int> q;
        vector<bool> vis(n + 1);
        q.push(n);
        vis[n] = true;
        int level = 0;
        while(!q.empty()) {
            int sz = q.size();
            level++;
            while(sz--) {
                int cur = q.front();
                q.pop();
                for(int i = 1; i * i <= cur; i++) {
                    int next = cur - i * i;
                    if(next == 0) return level;
                    if(!vis[next]) {
                        vis[next] = true;
                        q.push(next);
                    }
                }
            }
        }
        return level;
    }
};

๐Ÿ“ 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:

  1. Check if n is perfect square - return 1.

  2. Remove all factors of 4 from n.

  3. If remaining number โ‰ก 7 (mod 8), return 4.

  4. Otherwise check for sum of two squares, else return 3.

class Solution {
public:
    int minSquares(int n) {
        int s = sqrt(n);
        if(s * s == n) return 1;
        int temp = n;
        while(temp % 4 == 0) temp /= 4;
        if(temp % 8 == 7) return 4;
        for(int i = 1; i * i <= n; i++)
            if(s = sqrt(n - i * i); s * s == n - i * i) return 2;
        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)

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

Visitor counter

Last updated