C++ solution
\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        unordered_map<int,int>m;\n        \n        // \'sum\' will store 
```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        unordered_map<int,int>m;\n        \n        // \'sum\' will store the current sum of the elements\n        int sum=0,n=arr.size();\n        \n        // \'ans\' will be the ans\n        long long ans=INT_MAX;\n        \n        // We\'ll build two arrays prefix and suffix\n        // prefix[i] = minimum length of subarray ending on or before index i with sum = target\n        // suffix[i] = minimum length of subarray starting from or after index i with sum = target\n        vector<int>prefix(n,INT_MAX),suffix(n,INT_MAX);\n        \n        // Now populate the prefix array\n\t\t// Logic to populate the array is similar to finding the smallest subarray with a target sum\n        for(int i=0;i<n;i++)\n        {\n            sum+=arr[i];\n            if(sum==target)\n            {\n                prefix[i]=min(prefix[i],i+1);\n            }\n            if(m.find(sum-target)!=m.end())\n            {\n                prefix[i]=min(prefix[i],i-m[sum-target]);\n            }\n            prefix[i]=i>0?min(prefix[i],prefix[i-1]):prefix[i];\n            m[sum]=i;\n        }\n        m.clear();\n        sum=0;\n        \n        // Populate the suffix array\n        for(int i=n-1;i>=0;i--)\n        {\n            sum+=arr[i];\n            if(sum==target)\n            {\n                suffix[i]=min(suffix[i],n-i);\n            }\n            if(m.find(sum-target)!=m.end())\n            {\n                suffix[i]=min(suffix[i],m[sum-target]-i);\n            }\n            suffix[i]=i+1<n?min(suffix[i],suffix[i+1]):suffix[i];\n            m[sum]=i;\n        }\n        \n        // Now once we\'ve got the prefix and suffix array we will traverse from 1 to n-1 and compute the answer\n        for(int i=1;i<n-1;i++)\n        {\n            // first find left subarray ending on or before index \'i\' and right subarray starting from or after index \'i+1\'\n            long long first=(long long)prefix[i]+(long long)suffix[i+1];\n            \n            // second find left subarray ending on or before index \'i-1\' and right subarray starting from or after index \'i\'\n            long long second=(long long)prefix[i-1]+(long long)suffix[i];\n            \n            // take whichever gives the min aggregate length\n            ans=min({ans,first,second});\n        }\n        \n        // if at the end the ans is INT_MAX or greater that means there were no 2 subarrys with sum = target so return -1. otherwise return the actual ans\n        return ans>=INT_MAX?-1:ans;\n    }\n};\n```\n***Please upvote if you have got any help from my code. Thank you.***
['C', 'Suffix Array']