{"record":{"id":"5e826f65f5186999","repo":"TheAlgorithms/JavaScript","slug":"subarray-size-k-must-be-between-1-and-the-length-o","errorCode":null,"errorMessage":"Subarray size k must be between 1 and the length of the array","messagePattern":"Subarray size k must be between 1 and the length of the array","errorType":"exception","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Sliding-Windows/MaxSumSubarrayFixed.js","lineNumber":11,"sourceCode":"/**\n * Function to find the maximum sum of a subarray of fixed size k.\n *\n * @param {number[]} arr - The input array of numbers.\n * @param {number} k - The fixed size of the subarray.\n * @returns {number} - The maximum sum of any subarray of size k.\n * @throws {RangeError} - If k is larger than the array length or less than 1.\n */\nexport function maxSumSubarrayFixed(arr, k) {\n  if (k > arr.length || k < 1) {\n    throw new RangeError(\n      'Subarray size k must be between 1 and the length of the array'\n    )\n  }\n  let maxSum = 0\n  let windowSum = 0\n  for (let i = 0; i < k; i++) {\n    windowSum += arr[i]\n  }\n  maxSum = windowSum\n  for (let i = k; i < arr.length; i++) {\n    windowSum += arr[i] - arr[i - k]\n    maxSum = Math.max(maxSum, windowSum)\n  }\n  return maxSum\n}\n","sourceCodeStart":1,"sourceCodeEnd":27,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Sliding-Windows/MaxSumSubarrayFixed.js#L1-L27","documentation":"Thrown as a RangeError by maxSumSubarrayFixed() when k is greater than the array length or less than 1. The sliding window needs exactly k elements to form a window, so k must be a positive integer no larger than the array. The guard is deliberately a RangeError (not TypeError) because the value type is fine but its magnitude is wrong.","triggerScenarios":"Calling maxSumSubarrayFixed([1,2,3], 5), maxSumSubarrayFixed([1,2,3], 0), maxSumSubarrayFixed([], 1), or maxSumSubarrayFixed([1,2], -1). Also when k is derived from user input that was not sanitized.","commonSituations":"Computing window size from a ratio that exceeds array length; passing k=0 as a 'no window' sentinel; empty arrays after filtering where k was sized for the original array.","solutions":["Clamp k: const safeK = Math.min(k, arr.length) and ensure safeK >= 1 before calling.","Skip the call for arrays shorter than your desired window.","Validate user-supplied window sizes at the input boundary."],"exampleFix":"// before\nconst best = maxSumSubarrayFixed(arr, windowSize)\n\n// after\nconst safeK = Math.max(1, Math.min(windowSize, arr.length))\nconst best = arr.length >= safeK ? maxSumSubarrayFixed(arr, safeK) : 0","handlingStrategy":"validation","validationCode":"function safeMaxSumSubarrayFixed(arr, k) {\n  if (!Array.isArray(arr) || arr.length === 0) {\n    throw new TypeError('arr must be a non-empty array')\n  }\n  if (!Number.isInteger(k) || k < 1 || k > arr.length) {\n    throw new RangeError(`k must be in [1, ${arr.length}]`)\n  }\n  return maxSumSubarrayFixed(arr, k)\n}","typeGuard":"function isValidWindow(arr, k) {\n  return Array.isArray(arr) && arr.length > 0 && Number.isInteger(k) && k >= 1 && k <= arr.length\n}","tryCatchPattern":"try {\n  maxSumSubarrayFixed(arr, k)\n} catch (e) {\n  if (e instanceof RangeError) {\n    return maxSumSubarrayFixed(arr, Math.min(Math.max(1, k), arr.length))\n  }\n  throw e\n}","preventionTips":["Clamp k to [1, arr.length] before calling.","Skip short arrays rather than passing an oversized window.","Validate window sizes derived from user input or ratios."],"tags":["range-check","sliding-window","input-validation"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}