{"record":{"id":"4b27156bffbe707d","repo":"TheAlgorithms/JavaScript","slug":"error-size-should-be-less-than-equal-to-32","errorCode":null,"errorMessage":"Error size should be less than equal to 32","messagePattern":"Error size should be less than equal to 32","errorType":"validation","errorClass":"RangeError","httpStatus":null,"severity":"error","filePath":"Bit-Manipulation/GenerateSubSets.js","lineNumber":14,"sourceCode":"/**\n * @function generateSubSets\n * @param {Array} inputArray\n * @returns {Array}\n * @example [1,2] -> [[],[1],[2],[1,2]]\n */\n\n// The time complexity of this algorithm is BigO(2^n) where n is the length of array\nfunction generateSubSets(inputArray) {\n  if (!Array.isArray(inputArray)) {\n    throw new Error('Provided input is not an array')\n  }\n  if (inputArray.length > 32) {\n    throw new RangeError('Error size should be less than equal to 32')\n  }\n  let arrayLength = inputArray.length\n  let subSets = []\n  // loop till (2^n) - 1\n  for (let i = 0; i < 1 << arrayLength; i++) {\n    let subSet = []\n    for (let j = 0; j < arrayLength; j++) {\n      // 1 << j it shifts binary digit 1 by j positions and then we perform\n      // and by AND operation we are checking whetheer jth bit\n      // in i is set to 1 if result is non zero just add into set\n      if (i & (1 << j)) {\n        subSet.push(inputArray[j])\n      }\n    }\n    subSets.push(subSet)\n  }\n  return subSets\n}","sourceCodeStart":1,"sourceCodeEnd":32,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Bit-Manipulation/GenerateSubSets.js#L1-L32","documentation":"generateSubSets iterates from 0 to 2^n - 1 using the bitmask (1 << n), so n > 32 both produces an astronomically large result set (>4 billion subsets) and overflows JS 32-bit bitwise shift behavior (1 << 32 === 1, corrupting the loop). The 32-element cap prevents hang/crash.","triggerScenarios":"Passing an array with 33 or more elements.","commonSituations":"Feeding a large dataset, a full wordlist, or an unfiltered collection expecting the full power set without realizing it is O(2^n).","solutions":["Reduce the input to 32 elements or fewer before calling.","If you only need subset counts (not the subsets themselves), use the combinatorial 2^n formula instead.","For genuinely large power sets, implement a streaming/generator approach with BigInt-indexed batching."],"exampleFix":"// before\ngenerateSubSets(bigArray) // bigArray.length === 50\n// after\ngenerateSubSets(bigArray.slice(0, 32))","handlingStrategy":"validation","validationCode":"function subsetsBounded(arr, max = 32) {\n  if (arr.length > max) {\n    throw new RangeError(`Array too large (${arr.length} > ${max}) for power-set generation`);\n  }\n  return generateSubSets(arr);\n}","typeGuard":"/** @param {unknown[]} arr @returns {boolean} */\nconst withinPowerSetLimit = arr => Array.isArray(arr) && arr.length <= 32;","tryCatchPattern":"try { return generateSubSets(arr); }\ncatch (e) {\n  if (e instanceof RangeError && /less than equal to 32/.test(e.message)) {\n    return generateSubSets(arr.slice(0, 32)); // or compute count via 2**n\n  }\n  throw e;\n}","preventionTips":["Cap input size before calling; power-set is O(2^n).","Prefer the 2**n count formula if you only need the number of subsets.","Stream/generate subsets lazily for large n instead of materializing all."],"tags":["bit-manipulation","power-set","range-validation","performance"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}