{"record":{"id":"68908486622678cf","repo":"TheAlgorithms/Python","slug":"either-the-item-is-not-in-the-array-or-the-array-w","errorCode":null,"errorMessage":"Either the item is not in the array or the array was unsorted","messagePattern":"Either the item is not in the array or the array was unsorted","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"other/number_container_system.py","lineNumber":73,"sourceCode":"            array = list(array)\n        elif not isinstance(array, list):\n            raise TypeError(\n                \"binary_search_delete() only accepts either a list, range or str\"\n            )\n\n        low = 0\n        high = len(array) - 1\n\n        while low <= high:\n            mid = (low + high) // 2\n            if array[mid] == item:\n                array.pop(mid)\n                return array\n            elif array[mid] < item:\n                low = mid + 1\n            else:\n                high = mid - 1\n        raise ValueError(\n            \"Either the item is not in the array or the array was unsorted\"\n        )\n\n    def binary_search_insert(self, array: list | str | range, index: int) -> list[int]:\n        \"\"\"\n        Inserts the index into the sorted array\n        at the correct position.\n\n        >>> NumberContainer().binary_search_insert([1,2,3], 2)\n        [1, 2, 2, 3]\n        >>> NumberContainer().binary_search_insert([0,1,3], 2)\n        [0, 1, 2, 3]\n        >>> NumberContainer().binary_search_insert([-5, -3, 0, 0, 11, 103], 51)\n        [-5, -3, 0, 0, 11, 51, 103]\n        >>> NumberContainer().binary_search_insert([-5, -3, 0, 0, 11, 100, 103], 101)\n        [-5, -3, 0, 0, 11, 100, 101, 103]\n        >>> NumberContainer().binary_search_insert(range(10), 4)\n        [0, 1, 2, 3, 4, 4, 5, 6, 7, 8, 9]","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/other/number_container_system.py#L55-L91","documentation":"Raised by NumberContainer.binary_search_delete after the search loop terminates without finding the item. Because binary search only visits the positions a sorted array would place the item, this fires both when the item is genuinely absent and when the array is not sorted — the message intentionally covers both causes.","triggerScenarios":"Calling binary_search_delete([2, 0, 4, -1, 11], -1) (unsorted input — the doctest itself shows this failing), or searching for a value that does not exist in an otherwise sorted array.","commonSituations":"Forgetting that the container assumes sorted order (e.g. appending values without binary_search_insert), or deleting an item already removed by an earlier operation.","solutions":["Sort the array before deleting: array.sort() then binary_search_delete(array, item)","Use binary_search_insert to maintain sorted order incrementally instead of plain append","If absence is expected, wrap the call in try/except ValueError or check `item in array` first (linear but correct for unsorted data)"],"exampleFix":"# before\nNumberContainer().binary_search_delete([2, 0, 4, -1, 11], -1)  # unsorted -> ValueError\n\n# after\narr = sorted([2, 0, 4, -1, 11])\nNumberContainer().binary_search_delete(arr, -1)","handlingStrategy":"validation","validationCode":"def can_binary_delete(array: list, item) -> bool:\n    return array == sorted(array) and item in array  # cheap guard; O(n log n)","typeGuard":"def is_sorted(array: list) -> bool:\n    return all(array[i] <= array[i + 1] for i in range(len(array) - 1))","tryCatchPattern":"try:\n    array = container.binary_search_delete(array, item)\nexcept ValueError as e:\n    if 'not in the array' in str(e):\n        pass  # absent item: nothing to delete\n    else:\n        raise","preventionTips":["Maintain sorted order with binary_search_insert; never append raw values","Sort once at ingestion and assert is_sorted before binary operations"],"tags":["binary-search","sorted-input","value-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}