Javascript O(n) with Hashmap, and sliding window
These are Java solutions with great votes. I\'ve just converted them in to Javascript\n\nHashmap\n\nfunction minSumOfLengths(arr, target) {\n  let hmap = new Ma
These are Java solutions with great votes. I\'ve just converted them in to Javascript\n\n**Hashmap**\n```\nfunction minSumOfLengths(arr, target) {\n  let hmap = new Map();\n  let sum = 0, lsize = Number.MAX_VALUE, result = Number.MAX_VALUE;\n  hmap.set(0, -1);\n  for (let i = 0; i < arr.length; i++) {\n    sum += arr[i];\n    hmap.set(sum, i); // stores key as sum upto index i, and value as i.\n  }\n  sum = 0;\n  for (let i = 0; i < arr.length; i++) {\n    sum += arr[i];\n    if (hmap.get(sum - target) != null) {\n      lsize = Math.min(lsize, i - hmap.get(sum - target));      // stores minimum length of sub-array ending with index<= i with sum target. This ensures non- overlapping property.\n    }\n    //hmap.get(sum+target) searches for any sub-array starting with index i+1 with sum target.\n    if (hmap.get(sum + target) != null && lsize < Number.MAX_VALUE) {\n      result = Math.min(result, hmap.get(sum + target) - i + lsize); // updates the result only if both left and right sub-array exists.\n    }\n  }\n  return result == Number.MAX_VALUE ? -1 : result;\n}\n```\n\n**Sliding Window**\n```\nvar minSumOfLengths = function (arr, target) {\n  let n = arr.length;\n  let best = new Array(n).fill(Number.MAX_VALUE);\n  let sum = 0, start = 0, ans = Number.MAX_VALUE, bestSoFar = Number.MAX_VALUE;\n  for (let i = 0; i < n; i++) {\n    sum += arr[i];\n    while (sum > target) {\n      sum -= arr[start];\n      start++;\n    }\n    if (sum == target) {\n      if (start > 0 && best[start - 1] != Number.MAX_VALUE) {\n        ans = Math.min(ans, best[start - 1] + i - start + 1);\n      }\n      bestSoFar = Math.min(bestSoFar, i - start + 1);\n    }\n    best[i] = bestSoFar;\n  }\n  return ans == Number.MAX_VALUE ? -1 : ans;\n};\n```
['Sliding Window', 'JavaScript']