{"record":{"id":"9d32d193bd7bc90d","repo":"TheAlgorithms/JavaScript","slug":"triplet-cannot-exist-with-the-given-array","errorCode":null,"errorMessage":"Triplet cannot exist with the given array","messagePattern":"Triplet cannot exist with the given array","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"Dynamic-Programming/MaxProductOfThree.js","lineNumber":11,"sourceCode":"/**\n *  Given an array of numbers, return the maximum product\n *  of 3 numbers from the array\n *  https://wsvincent.com/javascript-three-sum-highest-product-of-three-numbers/\n * @param {number[]} arrayItems\n * @returns number\n */\nexport function maxProductOfThree(arrayItems) {\n  // if size is less than 3, no triplet exists\n  const n = arrayItems.length\n  if (n < 3) throw new Error('Triplet cannot exist with the given array')\n  let max1 = arrayItems[0]\n  let max2 = null\n  let max3 = null\n  let min1 = arrayItems[0]\n  let min2 = null\n  for (let i = 1; i < n; i++) {\n    if (arrayItems[i] > max1) {\n      max3 = max2\n      max2 = max1\n      max1 = arrayItems[i]\n    } else if (max2 === null || arrayItems[i] > max2) {\n      max3 = max2\n      max2 = arrayItems[i]\n    } else if (max3 === null || arrayItems[i] > max3) {\n      max3 = arrayItems[i]\n    }\n    if (arrayItems[i] < min1) {\n      min2 = min1","sourceCodeStart":1,"sourceCodeEnd":29,"githubUrl":"https://github.com/TheAlgorithms/JavaScript/blob/5c39e87a9a31f279c60f830ad74a845e4788a517/Dynamic-Programming/MaxProductOfThree.js#L1-L29","documentation":"Thrown by maxProductOfThree(arrayItems) (plain Error) when arrayItems.length < 3. The algorithm tracks the three largest and two smallest values in a single pass, which is only meaningful when at least three numbers exist; fewer than three means no triplet product is defined.","triggerScenarios":"maxProductOfThree([]); maxProductOfThree([5]); maxProductOfThree([1, 2]).","commonSituations":"Filtered/downsampled datasets that shrank below 3; empty input from a failed fetch; arrays built from a group-by whose group happened to have 1-2 members.","solutions":["Check arrayItems.length >= 3 before calling.","If a smaller array is legitimate in your domain, define a fallback (e.g. product of all elements) rather than calling the function.","Filter out degenerate groups before aggregating.","Validate input size at the API boundary and return a domain-specific error."],"exampleFix":"// before\nconst p = maxProductOfThree(arr) // throws when arr.length < 3\n\n// after\nif (arr.length < 3) throw new RangeError('need >= 3 numbers')\nconst p = maxProductOfThree(arr)","handlingStrategy":"validation","validationCode":"function safeMaxProductOfThree(arr) {\n  if (!Array.isArray(arr) || arr.length < 3) {\n    throw new RangeError('array must contain at least 3 numbers')\n  }\n  return maxProductOfThree(arr)\n}","typeGuard":"const hasTriplet = (arr) => Array.isArray(arr) && arr.length >= 3","tryCatchPattern":"try {\n  return maxProductOfThree(arr)\n} catch (e) {\n  if (e instanceof Error && /triplet/i.test(e.message)) {\n    // not enough elements; return a domain fallback or rethrow as a clearer error\n  } else throw e\n}","preventionTips":["Check arr.length >= 3 at the boundary.","Filter out degenerate/empty groups before aggregating.","Define a domain fallback for small arrays instead of calling the function.","Validate input size early so callers get a clear error."],"tags":["dynamic-programming","array","input-size","product"],"backgroundTag":null,"analyzedSha":"5c39e87a9a31f279c60f830ad74a845e4788a517","analyzedAt":"2026-08-13T04:54:54.474Z","schemaVersion":2},"datasetVersion":"2026-08-13T09:17:06.757Z"}