Java O(N) solution using prefixSum and HashMap
I create a prefixSum array and store the indices of prefixSum with the corresponding index. Then start a search for prefixSum[i] - target and prefixSum[i] + tar
I create a prefixSum array and store the indices of prefixSum with the corresponding index. Then start a search for prefixSum[i] - target and prefixSum[i] + target for each index in prefixSum to find the minimum left and right ranges respectively.\n\n```\nclass Solution {\n    public int minSumOfLengths(int[] arr, int target) {\n\t\tint[] prefixSum = new int[arr.length + 1];\n\t\tMap<Integer, Integer> indexMap = new HashMap<>();\n\t\tindexMap.put(0, -1);\n\t\tfor (int i = 0; i < arr.length; i++) {\n\t\t\tprefixSum[i + 1] = prefixSum[i] + arr[i];\n\t\t\tindexMap.put(prefixSum[i + 1], i);\n\t\t}\n\n\t\tint left = Integer.MAX_VALUE;\n\t\tint right = 0;\n\t\tint result = Integer.MAX_VALUE;\n\n\t\tfor (int i = 0; i < prefixSum.length; i++) {\n\t\t\tif (indexMap.containsKey(prefixSum[i] - target)) {\n\t\t\t\tint prevIdx = indexMap.get(prefixSum[i] - target);\n\t\t\t\tleft = Math.min(left, i - prevIdx);\n\t\t\t}\n\n\t\t\tif (indexMap.containsKey(prefixSum[i] + target)) {\n\t\t\t\tint nextIdx = indexMap.get(prefixSum[i] + target);\n\t\t\t\tright = nextIdx - i;\n\t\t\t\tif (left != Integer.MAX_VALUE) {\n\t\t\t\t\tresult = Math.min(result, right + left);\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\t\tif (result == Integer.MAX_VALUE)\n\t\t\treturn -1;\n\t\treturn result;\n\t}\n}\n```
[]