{"record":{"id":"29cfecd4cccb2601","repo":"TheAlgorithms/Java","slug":"input-arrays-are-not-sorted","errorCode":null,"errorMessage":"Input arrays are not sorted","messagePattern":"Input arrays are not sorted","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java","lineNumber":51,"sourceCode":"\n            // Check if partition is valid\n            if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {\n                // If combined array length is odd\n                if (((m + n) & 1) == 1) {\n                    return Math.max(maxLeft1, maxLeft2);\n                }\n                // If combined array length is even\n                else {\n                    return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2.0;\n                }\n            } else if (maxLeft1 > minRight2) {\n                high = partition1 - 1;\n            } else {\n                low = partition1 + 1;\n            }\n        }\n\n        throw new IllegalArgumentException(\"Input arrays are not sorted\");\n    }\n}\n","sourceCodeStart":33,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/divideandconquer/MedianOfTwoSortedArrays.java#L33-L54","documentation":"findMedianSortedArrays uses a binary-search-over-partition algorithm that is guaranteed to find a valid partition if and only if both input arrays are individually sorted in non-decreasing order. If the while loop exits without returning, the precondition was violated, so the method throws IllegalArgumentException indicating the arrays are not sorted.","triggerScenarios":"Passing arrays where at least one is not sorted in non-decreasing order, or arrays sorted in descending order. The binary search never converges on a valid partition and the loop terminates normally.","commonSituations":"Feeding raw unsorted data from a database or sensor stream; accidentally reversing an array; using descending-sorted data when ascending is required.","solutions":["Sort both arrays with Arrays.sort() before calling findMedianSortedArrays.","Validate sortedness with a pre-condition check before the call.","Review upstream data pipeline to ensure sorting is preserved end-to-end."],"exampleFix":"// before\ndouble m = MedianOfTwoSortedArrays.findMedianSortedArrays(\n    new int[]{3,1,2}, new int[]{6,5,4}); // throws\n\n// after\nint[] a = {3,1,2};\nint[] b = {6,5,4};\nArrays.sort(a);\nArrays.sort(b);\ndouble m = MedianOfTwoSortedArrays.findMedianSortedArrays(a, b);","handlingStrategy":"validation","validationCode":"static boolean isSorted(int[] arr) {\n    for (int i = 1; i < arr.length; i++) {\n        if (arr[i - 1] > arr[i]) return false;\n    }\n    return true;\n}\n// usage:\nif (!isSorted(nums1) || !isSorted(nums2)) {\n    Arrays.sort(nums1);\n    Arrays.sort(nums2);\n}\ndouble median = MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2);","typeGuard":"static boolean areBothSorted(int[] a, int[] b) {\n    return isSorted(a) && isSorted(b);\n}","tryCatchPattern":"try {\n    median = MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2);\n} catch (IllegalArgumentException e) {\n    Arrays.sort(nums1);\n    Arrays.sort(nums2);\n    median = MedianOfTwoSortedArrays.findMedianSortedArrays(nums1, nums2);\n}","preventionTips":["Sort arrays at the data-ingestion boundary so all downstream consumers can assume sortedness.","Document and enforce the sorted precondition with a wrapper method that sorts defensively."],"tags":["median","sorted-arrays","precondition","divide-and-conquer","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}