TheAlgorithms/Java · error · IllegalArgumentException

Invalid input

Error message

Invalid input

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/others/ArrayRightRotation.java:23

 * A left rotation operation shifts each element of the array
 * by a specified number of positions to the right.
 *
 * https://en.wikipedia.org/wiki/Right_rotation *
 */
public final class ArrayRightRotation {
    private ArrayRightRotation() {
    }

    /**
     * Performs a right rotation on the given array by the specified number of positions.
     *
     * @param arr the array to be rotated
     * @param k the number of positions to rotate the array to the left
     * @return a new array containing the elements of the input array rotated to the left
     */
    public static int[] rotateRight(int[] arr, int k) {
        if (arr == null || arr.length == 0 || k < 0) {
            throw new IllegalArgumentException("Invalid input");
        }

        int n = arr.length;
        k = k % n; // Handle cases where k is larger than the array length

        reverseArray(arr, 0, n - 1);
        reverseArray(arr, 0, k - 1);
        reverseArray(arr, k, n - 1);

        return arr;
    }

    /**
     * Performs reversing of a array
     * @param arr the array to be reversed
     * @param start starting position
     * @param end ending position
     */

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Guard the array for null/empty before calling, or branch around it.
  2. Normalize k into a non-negative range: k = ((k % n) + n) % n before the call.
  3. Log arr.length and k at the call site to identify which precondition failed.

Example fix

// before
int[] r = ArrayRightRotation.rotateRight(arr, offset); // offset may be negative

// after
if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Cannot rotate a null/empty array");
int k = ((offset % arr.length) + arr.length) % arr.length;
int[] r = ArrayRightRotation.rotateRight(arr, k);
Defensive patterns

Strategy: validation

Validate before calling

if (arr == null || arr.length == 0) {
    throw new IllegalArgumentException("Cannot rotate a null/empty array");
}
int k = ((offset % arr.length) + arr.length) % arr.length;
int[] r = ArrayRightRotation.rotateRight(arr, k);

Type guard

static boolean isRotatable(int[] a, int k) {
    return a != null && a.length > 0 && k >= 0;
}

Prevention

When it happens

Trigger: 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).

Common situations: Uninitialized array, empty source collection converted to array, or a rotation count derived from a subtraction/offset that went negative.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/9a20c072ad3da59c. Report an issue: GitHub.