C++ | Prefix sum | Hash | Two Pass
Based on C++ version of @pramitb\'s JAVA solution\n\n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        \n        int
Based on C++ version of @pramitb\'s JAVA solution\n\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        \n        int n = arr.size();\n        unordered_map<int, int>hm;\n        hm[0] = -1;\n        int sum = 0;\n        \n        for (int i = 0; i < n; i++) {\n            sum += arr[i];\n            hm[sum] = i;\n        }\n        \n        sum = 0;\n        int msize = INT_MAX, res = INT_MAX;\n        for (int i = 0; i < n; i++) {\n            sum += arr[i];\n            \n            if (hm.find(sum-target) != hm.end())\n                msize = min(msize, i-hm[sum-target]);\n            \n            if (hm.find(sum+target) != hm.end() && msize != INT_MAX)\n                res = min(res, msize + hm[sum+target]-i);\n        }\n        \n        return res == INT_MAX ? -1 : res;\n    }\n};\n```
[]