Easy C++ Solution
\n\n# Code\n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) \n    {      \n        map<int,int>mp,mp2;\n        int n=arr.siz
\n\n# Code\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) \n    {      \n        map<int,int>mp,mp2;\n        int n=arr.size();\n        int sum=0;\n        mp[0]=-1;\n        int mn=INT_MAX;\n        vector<int>pre(n),suff(n);\n        for (int i=0;i<n;i++)\n        {\n            sum+=arr[i];\n            int temp=INT_MAX;\n            if (mp.find(sum-target)!=mp.end())\n            {\n               int ind=mp[sum-target];\n               temp=i-ind;\n            }\n            mn=min(mn,temp);\n            pre[i]=mn;\n            mp[sum]=i;\n        }\n        sum=0;\n        mp2[0]=n;\n        mn=INT_MAX;\n        for (int i=n-1;i>=0;i--)\n        {\n            int temp=INT_MAX;\n            if (mp2.find(sum-target)!=mp2.end())\n            {\n                int ind=mp2[sum-target];\n                temp=ind-i-1;\n            }\n            mn=min(mn,temp);\n            suff[i]=mn;\n            sum+=arr[i];\n           mp2[sum]=i;\n        }\n        int ans=INT_MAX;\n        for (int i=0;i<n;i++)\n        {\n         //cout<<pre[i]<<" "<<suff[i]<<endl;\n        if (suff[i]==INT_MAX || pre[i]==INT_MAX) continue;\n\n        ans=min(ans,suff[i]+pre[i]);\n        }\n        if (ans==INT_MAX) return -1;\n        return ans;\n    }\n};\n```
['C++']