Hashmap with O(N) time
\n# Code\n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();\n\n        unordered_map<int,int> mp
\n# Code\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();\n\n        unordered_map<int,int> mp;\n        mp[0] = -1;\n\n        int sum = 0;\n\n        // first mapping all the prefix sum indices\n        for(int i=0;i<n;i++) {\n            sum += arr[i];\n            mp[sum] = i;\n        }\n\n        int len1 = INT_MAX, res = INT_MAX;\n        sum = 0;\n        // the basic idea is to get the target sum with minimum length subarray from current index to left and right\n        for(int i=0;i<n;i++) {\n            sum += arr[i];\n\n            if(mp.find(sum-target) != mp.end()) {       // to the left\n                len1 = min(len1,i-mp[sum-target]);\n            }\n\n            if(mp.find(sum+target) != mp.end() && len1 != INT_MAX) {     // to the right\n                int len2 = mp[sum+target]-i;\n                res = min(res, len1+len2);       // also need to check whether len != INT_MAX bcz, we haven\'t even got the target sum on the left and we are looking for right(we have to find two subarrays so left would be the first priority) \n            }\n\n        }\n        return (res == INT_MAX?-1:res);\n\n    }\n};\n```
['Hash Table', 'C++']