TheAlgorithms/Java · error · IllegalArgumentException

The number of scores must be a power of 2.

Error message

The number of scores must be a power of 2.

What it means

Thrown by the MiniMaxAlgorithm constructor when the scores array length is not a power of 2. The algorithm builds a complete binary game tree whose leaf count must equal the scores length, and the tree height is computed as log2(length); only power-of-2 lengths yield an integer height and a balanced tree.

Source

Thrown at src/main/java/com/thealgorithms/others/MiniMaxAlgorithm.java:65

    /**
     * Initializes the MiniMaxAlgorithm with 8 random leaf nodes (2^3 = 8).
     * Each score is a random integer between 1 and 99 inclusive.
     */
    public MiniMaxAlgorithm() {
        this(getRandomScores(3, 99));
    }

    /**
     * Initializes the MiniMaxAlgorithm with the provided scores.
     *
     * @param scores An array of scores representing leaf nodes. The length must be
     *               a power of 2.
     * @throws IllegalArgumentException if the scores array length is not a power of
     *                                  2
     */
    public MiniMaxAlgorithm(int[] scores) {
        if (!isPowerOfTwo(scores.length)) {
            throw new IllegalArgumentException("The number of scores must be a power of 2.");
        }
        this.scores = Arrays.copyOf(scores, scores.length);
        this.height = log2(scores.length);
    }

    /**
     * Demonstrates the MiniMax algorithm with a random game tree.
     *
     * @param args Command line arguments (not used)
     */
    public static void main(String[] args) {
        MiniMaxAlgorithm miniMaxAlgorithm = new MiniMaxAlgorithm();
        boolean isMaximizer = true; // Specifies the player that goes first.
        int bestScore;

        bestScore = miniMaxAlgorithm.miniMax(0, isMaximizer, 0, true);

        System.out.println();

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pad (or trim) the scores array to the nearest power of 2 before constructing.
  2. Choose a leaf count that is a power of 2 (1, 2, 4, 8, 16, 32, ...).
  3. If your data is not a power of 2, design around it (e.g. add neutral sentinel scores) before calling the constructor.
  4. Validate scores.length with a power-of-2 check at the data-prep stage.

Example fix

// before
int[] scores = {3, 5, 2, 9, 7}; // length 5 -> throws
new MiniMaxAlgorithm(scores);

// after
// pad to next power of 2 (8), filling extra leaves with a sentinel
int[] scores = {3, 5, 2, 9, 7, 0, 0, 0};
new MiniMaxAlgorithm(scores);
Defensive patterns

Strategy: validation

Validate before calling

public static boolean isPowerOfTwo(int n) {
    return n > 0 && (n & (n - 1)) == 0;
}
if (!isPowerOfTwo(scores.length)) {
    int next = Integer.highestOneBit(scores.length);
    if (next < scores.length) next <<= 1;
    scores = Arrays.copyOf(scores, next); // pad with zeros to a power of 2
}
new MiniMaxAlgorithm(scores);

Type guard

public static boolean isPowerOfTwoLength(int[] scores) {
    return scores != null && scores.length > 0 && (scores.length & (scores.length - 1)) == 0;
}

Try / catch

try {
    algo = new MiniMaxAlgorithm(scores);
} catch (IllegalArgumentException e) {
    int next = Integer.highestOneBit(scores.length);
    if (next < scores.length) next <<= 1;
    algo = new MiniMaxAlgorithm(Arrays.copyOf(scores, next));
}

Prevention

When it happens

Trigger: Constructing new MiniMaxAlgorithm(scores) where scores.length is 3, 5, 6, 7, 9, etc. (any value not 1, 2, 4, 8, 16, ...).

Common situations: Passing an arbitrary list of scores from user input; an odd number of game outcomes; truncating/padding a dataset to a non-power-of-2 size; misunderstanding that the tree must be full/balanced.

Related errors


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