{"record":{"id":"81c65d89a92582b4","repo":"TheAlgorithms/Java","slug":"frequencies-cannot-be-negative","errorCode":null,"errorMessage":"Frequencies cannot be negative","messagePattern":"Frequencies cannot be negative","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java","lineNumber":97,"sourceCode":"                    }\n                }\n            }\n        }\n\n        return optimalCost[0][nodeCount - 1];\n    }\n\n    private static void validateInput(int[] keys, int[] frequencies) {\n        if (keys == null || frequencies == null) {\n            throw new IllegalArgumentException(\"Keys and frequencies cannot be null\");\n        }\n        if (keys.length != frequencies.length) {\n            throw new IllegalArgumentException(\"Keys and frequencies must have the same length\");\n        }\n\n        for (int frequency : frequencies) {\n            if (frequency < 0) {\n                throw new IllegalArgumentException(\"Frequencies cannot be negative\");\n            }\n        }\n    }\n\n    private static int[][] sortNodes(int[] keys, int[] frequencies) {\n        int[][] sortedNodes = new int[keys.length][2];\n        for (int index = 0; index < keys.length; index++) {\n            sortedNodes[index][0] = keys[index];\n            sortedNodes[index][1] = frequencies[index];\n        }\n\n        // Sort by key so the nodes can be treated as an in-order BST sequence.\n        Arrays.sort(sortedNodes, Comparator.comparingInt(node -> node[0]));\n\n        for (int index = 1; index < sortedNodes.length; index++) {\n            if (sortedNodes[index - 1][0] == sortedNodes[index][0]) {\n                throw new IllegalArgumentException(\"Keys must be distinct\");\n            }","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/dynamicprogramming/OptimalBinarySearchTree.java#L79-L115","documentation":"Thrown by OptimalBinarySearchTree.validateInput when any frequency is negative. Frequencies represent access weights/costs and are summed into prefix sums; negative frequencies are meaningless for an OBST cost model. Message: 'Frequencies cannot be negative'.","triggerScenarios":"Passing frequencies containing a negative value; using -1 as a 'missing' marker in frequency data; signed-int parsing of unsigned source data.","commonSituations":"Aggregating counts where a bug subtracted too much; reading frequencies from a config with a typo'd negative number.","solutions":["Validate every frequency >= 0 at the data boundary.","Replace sentinel/missing markers with 0 or filter the entry out entirely.","Add assertions in the data-ingest layer."],"exampleFix":"// before\nint[] freq = {3, -1, 5};\n\n// after\nfor (int f : freq) {\n    if (f < 0) throw new IllegalArgumentException(\"negative freq: \" + f);\n}","handlingStrategy":"validation","validationCode":"for (int f : frequencies) {\n    if (f < 0) throw new IllegalArgumentException(\"negative frequency: \" + f);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never use negative numbers as frequency sentinels.","Validate at the ingest boundary.","Log the offending index when a negative is found."],"tags":["input-validation","array","negative-values","dynamic-programming"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}