{"record":{"id":"885d454d9ddc0425","repo":"TheAlgorithms/Java","slug":"the-number-of-scores-must-be-a-power-of-2","errorCode":null,"errorMessage":"The number of scores must be a power of 2.","messagePattern":"The number of scores must be a power of 2\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/MiniMaxAlgorithm.java","lineNumber":65,"sourceCode":"    /**\n     * Initializes the MiniMaxAlgorithm with 8 random leaf nodes (2^3 = 8).\n     * Each score is a random integer between 1 and 99 inclusive.\n     */\n    public MiniMaxAlgorithm() {\n        this(getRandomScores(3, 99));\n    }\n\n    /**\n     * Initializes the MiniMaxAlgorithm with the provided scores.\n     *\n     * @param scores An array of scores representing leaf nodes. The length must be\n     *               a power of 2.\n     * @throws IllegalArgumentException if the scores array length is not a power of\n     *                                  2\n     */\n    public MiniMaxAlgorithm(int[] scores) {\n        if (!isPowerOfTwo(scores.length)) {\n            throw new IllegalArgumentException(\"The number of scores must be a power of 2.\");\n        }\n        this.scores = Arrays.copyOf(scores, scores.length);\n        this.height = log2(scores.length);\n    }\n\n    /**\n     * Demonstrates the MiniMax algorithm with a random game tree.\n     *\n     * @param args Command line arguments (not used)\n     */\n    public static void main(String[] args) {\n        MiniMaxAlgorithm miniMaxAlgorithm = new MiniMaxAlgorithm();\n        boolean isMaximizer = true; // Specifies the player that goes first.\n        int bestScore;\n\n        bestScore = miniMaxAlgorithm.miniMax(0, isMaximizer, 0, true);\n\n        System.out.println();","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/MiniMaxAlgorithm.java#L47-L83","documentation":"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.","triggerScenarios":"Constructing new MiniMaxAlgorithm(scores) where scores.length is 3, 5, 6, 7, 9, etc. (any value not 1, 2, 4, 8, 16, ...).","commonSituations":"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.","solutions":["Pad (or trim) the scores array to the nearest power of 2 before constructing.","Choose a leaf count that is a power of 2 (1, 2, 4, 8, 16, 32, ...).","If your data is not a power of 2, design around it (e.g. add neutral sentinel scores) before calling the constructor.","Validate scores.length with a power-of-2 check at the data-prep stage."],"exampleFix":"// before\nint[] scores = {3, 5, 2, 9, 7}; // length 5 -> throws\nnew MiniMaxAlgorithm(scores);\n\n// after\n// pad to next power of 2 (8), filling extra leaves with a sentinel\nint[] scores = {3, 5, 2, 9, 7, 0, 0, 0};\nnew MiniMaxAlgorithm(scores);","handlingStrategy":"validation","validationCode":"public static boolean isPowerOfTwo(int n) {\n    return n > 0 && (n & (n - 1)) == 0;\n}\nif (!isPowerOfTwo(scores.length)) {\n    int next = Integer.highestOneBit(scores.length);\n    if (next < scores.length) next <<= 1;\n    scores = Arrays.copyOf(scores, next); // pad with zeros to a power of 2\n}\nnew MiniMaxAlgorithm(scores);","typeGuard":"public static boolean isPowerOfTwoLength(int[] scores) {\n    return scores != null && scores.length > 0 && (scores.length & (scores.length - 1)) == 0;\n}","tryCatchPattern":"try {\n    algo = new MiniMaxAlgorithm(scores);\n} catch (IllegalArgumentException e) {\n    int next = Integer.highestOneBit(scores.length);\n    if (next < scores.length) next <<= 1;\n    algo = new MiniMaxAlgorithm(Arrays.copyOf(scores, next));\n}","preventionTips":["Always check the leaf count is 1, 2, 4, 8, 16, ... before constructing.","Pad with neutral/sentinel scores to the next power of 2.","The full binary tree requires a power-of-2 leaf count by design."],"tags":["validation","minimax","game-tree","input-validation","power-of-two"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}