{"record":{"id":"9a20c072ad3da59c","repo":"TheAlgorithms/Java","slug":"invalid-input","errorCode":null,"errorMessage":"Invalid input","messagePattern":"Invalid input","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/ArrayRightRotation.java","lineNumber":23,"sourceCode":" * A left rotation operation shifts each element of the array\n * by a specified number of positions to the right.\n *\n * https://en.wikipedia.org/wiki/Right_rotation *\n */\npublic final class ArrayRightRotation {\n    private ArrayRightRotation() {\n    }\n\n    /**\n     * Performs a right rotation on the given array by the specified number of positions.\n     *\n     * @param arr the array to be rotated\n     * @param k the number of positions to rotate the array to the left\n     * @return a new array containing the elements of the input array rotated to the left\n     */\n    public static int[] rotateRight(int[] arr, int k) {\n        if (arr == null || arr.length == 0 || k < 0) {\n            throw new IllegalArgumentException(\"Invalid input\");\n        }\n\n        int n = arr.length;\n        k = k % n; // Handle cases where k is larger than the array length\n\n        reverseArray(arr, 0, n - 1);\n        reverseArray(arr, 0, k - 1);\n        reverseArray(arr, k, n - 1);\n\n        return arr;\n    }\n\n    /**\n     * Performs reversing of a array\n     * @param arr the array to be reversed\n     * @param start starting position\n     * @param end ending position\n     */","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/ArrayRightRotation.java#L5-L41","documentation":"Thrown by ArrayRightRotation.rotateRight when arr is null, arr.length == 0, or k < 0. The method rotates in place using three reversals and indexes arr[0..n-1], so a null/empty array or negative shift breaks the reversal bounds. The error message is generic (\"Invalid input\") and does not distinguish which condition failed.","triggerScenarios":"Calling rotateRight(null, k), rotateRight(new int[0], k), or rotateRight(arr, -1). Also passing a negative k from an arithmetic underflow (e.g., k computed as a-b where a<b).","commonSituations":"Uninitialized array, empty source collection converted to array, or a rotation count derived from a subtraction/offset that went negative.","solutions":["Guard the array for null/empty before calling, or branch around it.","Normalize k into a non-negative range: k = ((k % n) + n) % n before the call.","Log arr.length and k at the call site to identify which precondition failed."],"exampleFix":"// before\nint[] r = ArrayRightRotation.rotateRight(arr, offset); // offset may be negative\n\n// after\nif (arr == null || arr.length == 0) {\n    throw new IllegalArgumentException(\"Cannot rotate a null/empty array\");\nint k = ((offset % arr.length) + arr.length) % arr.length;\nint[] r = ArrayRightRotation.rotateRight(arr, k);","handlingStrategy":"validation","validationCode":"if (arr == null || arr.length == 0) {\n    throw new IllegalArgumentException(\"Cannot rotate a null/empty array\");\n}\nint k = ((offset % arr.length) + arr.length) % arr.length;\nint[] r = ArrayRightRotation.rotateRight(arr, k);","typeGuard":"static boolean isRotatable(int[] a, int k) {\n    return a != null && a.length > 0 && k >= 0;\n}","tryCatchPattern":null,"preventionTips":["Normalize the rotation count into [0, n) before calling.","Guard for null/empty arrays at the boundary."],"tags":["array","precondition","rotation","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}