{"record":{"id":"0c6925fc55205e7c","repo":"TheAlgorithms/Python","slug":"invalid-input","errorCode":null,"errorMessage":"Invalid Input","messagePattern":"Invalid Input","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"maths/max_sum_sliding_window.py","lineNumber":31,"sourceCode":"def max_sum_in_array(array: list[int], k: int) -> int:\n    \"\"\"\n    Returns the maximum sum of k consecutive elements\n    >>> arr = [1, 4, 2, 10, 2, 3, 1, 0, 20]\n    >>> k = 4\n    >>> max_sum_in_array(arr, k)\n    24\n    >>> k = 10\n    >>> max_sum_in_array(arr,k)\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid Input\n    >>> arr = [1, 4, 2, 10, 2, 13, 1, 0, 2]\n    >>> k = 4\n    >>> max_sum_in_array(arr, k)\n    27\n    \"\"\"\n    if len(array) < k or k < 0:\n        raise ValueError(\"Invalid Input\")\n    max_sum = current_sum = sum(array[:k])\n    for i in range(len(array) - k):\n        current_sum = current_sum - array[i] + array[i + k]\n        max_sum = max(max_sum, current_sum)\n    return max_sum\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n    from random import randint\n\n    testmod()\n    array = [randint(-1000, 1000) for i in range(100)]\n    k = randint(0, 110)\n    print(\n        f\"The maximum sum of {k} consecutive elements is {max_sum_in_array(array, k)}\"\n    )\n","sourceCodeStart":13,"sourceCodeEnd":49,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/maths/max_sum_sliding_window.py#L13-L49","documentation":"Raised by max_sum_in_array() in maths/max_sum_sliding_window.py when len(array) < k or k < 0. The sliding-window algorithm needs at least one full window of size k inside the array, so a window larger than the array (or a negative size) makes the problem undefined and the function raises ValueError('Invalid Input').","triggerScenarios":"max_sum_in_array(arr, 10) where arr has 9 or fewer elements (e.g. the doctest case arr=[2,1,5,1,3,2], k=10), or any negative k such as max_sum_in_array(arr, -1).","commonSituations":"k computed from user input or from len(array) arithmetic (e.g. k = len(arr) + margin), small test fixtures with a hardcoded window size, or off-by-one errors when deriving k.","solutions":["Choose k <= len(array), typically k in [1, len(array)].","Clamp derived window sizes: k = max(1, min(k, len(array))).","Validate k against array length at the point where k is configured, not deep in the call stack."],"exampleFix":"# before\nmax_sum_in_array([2, 1, 5, 1, 3, 2], 10)  # window larger than array\n\n# after\nk = min(10, len(arr))\nmax_sum_in_array(arr, k)","handlingStrategy":"validation","validationCode":"if not 1 <= k <= len(array):\n    k = max(1, min(k, len(array)))  # or raise with your own message","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate window size against data length where k is configured.","Prefer computing k as a fraction of len(array) with clamping."],"tags":["math","algorithms","valueerror","sliding-window"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}