{"record":{"id":"4a2ddfe58e5f35be","repo":"TheAlgorithms/Java","slug":"input-must-be-non-negative-4a2ddf","errorCode":null,"errorMessage":"Input must be non-negative","messagePattern":"Input must be non-negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/maths/SumOfSquares.java","lineNumber":24,"sourceCode":" *\n * @see <a href=\"https://en.wikipedia.org/wiki/Lagrange%27s_four-square_theorem\">Lagrange's Four Square Theorem</a>\n */\npublic final class SumOfSquares {\n\n    private SumOfSquares() {\n        // Utility class\n    }\n\n    /**\n     * Find minimum number of perfect squares that sum to n\n     *\n     * @param n the target number (must be non-negative)\n     * @return minimum number of squares needed\n     * @throws IllegalArgumentException if n is negative\n     */\n    public static int minSquares(int n) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Input must be non-negative\");\n        }\n\n        if (isPerfectSquare(n)) {\n            return 1;\n        }\n\n        for (int i = 1; i * i <= n; i++) {\n            int remaining = n - i * i;\n            if (isPerfectSquare(remaining)) {\n                return 2;\n            }\n        }\n\n        // Legendre's three-square theorem\n        int temp = n;\n        while (temp % 4 == 0) {\n            temp /= 4;\n        }","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/maths/SumOfSquares.java#L6-L42","documentation":"Thrown by SumOfSquares.minSquares(int n) when n is negative. The method implements Lagrange's four-square theorem (every non-negative integer is a sum of <= 4 perfect squares), which is undefined for negatives; the guard prevents the algorithm from looping on invalid input.","triggerScenarios":"Call minSquares(-1) or pass a target computed from a subtraction that went negative.","commonSituations":"Untested numeric input, an expression like (target - offset) with offset > target, or porting unsigned-arithmetic assumptions.","solutions":["Validate n >= 0 before calling.","Use Math.max(0, n) if negatives should be treated as 0.","Surface the constraint to the user/API consumer explicitly."],"exampleFix":"// before\nint k = SumOfSquares.minSquares(n);\n\n// after\nif (n < 0) throw new IllegalArgumentException(\"n must be >= 0, got \" + n);\nint k = SumOfSquares.minSquares(n);","handlingStrategy":"validation","validationCode":"if (n < 0) throw new IllegalArgumentException(\"n must be >= 0\");\nint k = SumOfSquares.minSquares(n);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Number-theory utilities are almost always defined only on non-negative integers; validate accordingly.","Audit expressions like (target - offset) for underflow before they reach such APIs."],"tags":["math","number-theory","input-validation","argument-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}