C++ O(n) prefix sum
Do prefix sum from left to right and right to left.\n\nright array can be eliminated.\n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, in
Do prefix sum from left to right and right to left.\n\nright array can be eliminated.\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();\n        \n        vector<int> left(n);\n        unordered_map<int, int> preSums;\n        preSums[0] = -1;\n        for (int i = 0, preSum = 0, m = INT_MAX; i < n; i++) {\n            preSum += arr[i];\n            if (preSums.find(preSum - target) != preSums.end()) {\n                m = min(m, i - preSums[preSum - target]);\n            }\n            left[i] = m;\n            preSums[preSum] = i;\n        }\n        \n        vector<int> right(n);\n        unordered_map<int, int> postSums;\n        postSums[0] = n;\n        for (int i = n - 1, postSum = 0, m = INT_MAX; i >= 0; i--) {\n            postSum += arr[i];\n            if (postSums.find(postSum - target) != postSums.end()) {\n                m = min(m, postSums[postSum - target] - i);\n            }\n            right[i] = m;\n            postSums[postSum] = i;\n        }\n        \n        int result = INT_MAX;\n        for (int i = 0; i + 1 < n; i++) {\n            if (left[i] < INT_MAX && right[i + 1] < INT_MAX) {\n                result = min(result, left[i] + right[i + 1]);\n            }\n        }\n        if (result == INT_MAX) {\n            return -1;\n        } else {\n            return result;\n        }\n    }\n};\n```
[]