{"record":{"id":"7ecaecd12d73790b","repo":"TheAlgorithms/Java","slug":"number-of-discs-must-be-non-negative","errorCode":null,"errorMessage":"Number of discs must be non-negative","messagePattern":"Number of discs must be non-negative","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/puzzlesandgames/TowerOfHanoi.java","lineNumber":61,"sourceCode":"     * @param result           A list to store the steps required to solve the puzzle.\n     * @throws IllegalArgumentException if {@code n} is negative.\n     *\n     *                         <p>\n     *                         This method is called recursively to move n-1 discs\n     *                         to the intermediate pole,\n     *                         then moves the nth disc to the end pole, and finally\n     *                         moves the n-1 discs from the\n     *                         intermediate pole to the end pole.\n     *                         </p>\n     *\n     *                         <p>\n     *                         Time Complexity: O(2^n) - Exponential time complexity due to the recursive nature of the problem.\n     *                         Space Complexity: O(n) - Linear space complexity due to the recursion stack.\n     *                         </p>\n     */\n    public static void shift(int n, String startPole, String intermediatePole, String endPole, List<String> result) {\n        if (n < 0) {\n            throw new IllegalArgumentException(\"Number of discs must be non-negative\");\n        }\n        if (n == 0) {\n            return;\n        }\n\n        // Move n-1 discs from startPole to intermediatePole\n        shift(n - 1, startPole, endPole, intermediatePole, result);\n\n        // Add the move of the nth disc from startPole to endPole\n        result.add(String.format(\"Move %d from %s to %s\", n, startPole, endPole));\n\n        // Move the n-1 discs from intermediatePole to endPole\n        shift(n - 1, intermediatePole, startPole, endPole, result);\n    }\n}\n","sourceCodeStart":43,"sourceCodeEnd":77,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/puzzlesandgames/TowerOfHanoi.java#L43-L77","documentation":"Thrown by TowerOfHanoi.shift(n, ...) when the disc count n is negative. The recursion shift(n-1,...) would never terminate for n < 0 (deeper into negatives), so the guard rejects negative inputs. n == 0 is valid (no moves, empty result).","triggerScenarios":"Call shift(-1, ...) or shift(-5, ...). Any n < 0 trips it; the message says 'non-negative' and n=0 is permitted.","commonSituations":"Subtracting from an input that could go below zero (n - 1 where n was 0); parsing a negative disc count from config; pre-decrementing n before calling.","solutions":["Clamp n to >= 0 before calling: Math.max(0, n).","Validate n >= 0 at the caller and reject the request early with a domain error.","Check for n <= 0 and return an empty result list without invoking shift."],"exampleFix":"// before\nTowerOfHanoi.shift(n, \"A\", \"B\", \"C\", moves); // n may be negative\n\n// after\nif (n < 0) throw new IllegalArgumentException(\"disc count must be >= 0\");\nTowerOfHanoi.shift(n, \"A\", \"B\", \"C\", moves);","handlingStrategy":"validation","validationCode":"if (n < 0) {\n    throw new IllegalArgumentException(\"disc count must be non-negative\");\n}\nList<String> moves = new ArrayList<>();\nTowerOfHanoi.shift(n, \"A\", \"B\", \"C\", moves);","typeGuard":"static boolean validDiscCount(int n) {\n    return n >= 0;\n}","tryCatchPattern":"try {\n    TowerOfHanoi.shift(n, \"A\", \"B\", \"C\", moves);\n} catch (IllegalArgumentException e) {\n    logger.warn(\"Negative disc count: {}\", n);\n}","preventionTips":["Validate disc count at the input boundary.","Guard large n too — moves grow as 2^n and can exhaust memory/time.","Pre-check n <= 0 to return an empty move list without invoking recursion."],"tags":["puzzles","recursion","input-validation","illegal-argument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}