{"record":{"id":"27f5afdd9bef9df8","repo":"TheAlgorithms/Java","slug":"no-more-than-half-the-number-of-values-may-be-the","errorCode":null,"errorMessage":"No more than half the number of values may be the same.","messagePattern":"No more than half the number of values may be the same\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/sorts/WiggleSort.java","lineNumber":79,"sourceCode":"\n        int numMedians = 0;\n\n        for (T sortThi : sortThis) {\n            if (0 == sortThi.compareTo(median)) {\n                numMedians++;\n            }\n        }\n        // added condition preventing off-by-one errors for odd arrays.\n        // https://cs.stackexchange.com/questions/150886/how-to-find-wiggle-sortable-arrays-did-i-misunderstand-john-l-s-answer?noredirect=1&lq=1\n        if (sortThis.length % 2 == 1 && numMedians == ceil(sortThis.length / 2.0)) {\n            T smallestValue = select(Arrays.asList(sortThis), 0);\n            if (!(0 == smallestValue.compareTo(median))) {\n                throw new IllegalArgumentException(\"For odd Arrays if the median appears ceil(n/2) times, \"\n                    + \"the median has to be the smallest values in the array.\");\n            }\n        }\n        if (numMedians > ceil(sortThis.length / 2.0)) {\n            throw new IllegalArgumentException(\"No more than half the number of values may be the same.\");\n        }\n\n        triColorSort(sortThis, median);\n        return sortThis;\n    }\n}\n","sourceCodeStart":61,"sourceCodeEnd":86,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/sorts/WiggleSort.java#L61-L86","documentation":"A wiggle-sorted array needs strictly more non-median positions than median positions, otherwise there is no way to interleave the repeated median without two equal values becoming adjacent in a way that violates the <=/>= alternation. If the median value appears more than ceil(n/2) times, no valid arrangement exists, so WiggleSort throws IllegalArgumentException. This is the general (both odd and even length) duplicate-count guard, complementary to the odd-only check at line 74.","triggerScenarios":"Calling the sort on any array where one value (the median) appears more than ceil(n/2) times. Example: {1,1,1,1,2} -> median=1 appears 4 > ceil(5/2)=3 -> throws. Also {2,2,2} triggers this branch (3 > ceil(3/2)=2).","commonSituations":"Heavily skewed or constant arrays; sensor/telemetry data with a dominant reading; test fixtures using repetitive values; configs that pass the same default repeatedly.","solutions":["Reduce the count of the dominant value below the ceil(n/2) threshold by introducing distinct values.","Grow the array with distinct elements so no single value exceeds ceil(n/2) occurrences.","Pre-screen inputs and reject/report over-represented data instead of attempting the sort."],"exampleFix":"// before\nInteger[] a = {1, 1, 1, 1, 2}; // median appears 4 > ceil(5/2)=3\nWiggleSort.sort(a);            // throws\n\n// after\nInteger[] a = {1, 1, 1, 2, 3}; // median appears 3, equals threshold, ok\nWiggleSort.sort(a);","handlingStrategy":"validation","validationCode":"static <T extends Comparable<T>> boolean isWiggleSortable(T[] a) {\n    Map<T,Long> freq = Arrays.stream(a)\n        .collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));\n    long limit = (long) Math.ceil(a.length / 2.0);\n    return freq.values().stream().noneMatch(c -> c > limit);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Ensure no single value appears more than ceil(n/2) times.","Diversify heavily-tied datasets before wiggle sorting.","Validate duplicate distribution for any data fed from skewed sources."],"tags":["sorting","wiggle-sort","duplicates","validation","illegalargument"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}