Prefix sum and Sliding Window
\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n=arr.size();\n        vector<int> pref(n,INT_MAX),suff(n,INT_
```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n=arr.size();\n        vector<int> pref(n,INT_MAX),suff(n,INT_MAX);\n        int j=0,s=0;\n        for(int i=0;i<n;i++){\n            s+=arr[i];\n            while(j<=i && s>target){\n                s-=arr[j];\n                j++;\n            }\n            if(s==target){\n                if(i==0){\n                    pref[i]=(i-j+1);\n                    continue;\n                }\n                pref[i]=min(pref[i-1],i-j+1);\n            }else{\n                if(i!=0)\n                pref[i]=pref[i-1];\n            }\n        }\n        j=n-1,s=0;\n        for(int i=n-2;i>=0;i--){\n            s+=arr[i+1];\n            while(j>i && s>target){\n                s-=arr[j];\n                j--;\n            }\n            if(s==target){\n                suff[i]=min(suff[i+1],j-i);\n            }else{\n                suff[i]=suff[i+1];\n            }\n        }\n        int minl=INT_MAX;\n        for(int i=0;i<n;i++){\n            if(pref[i]!=INT_MAX && suff[i]!=INT_MAX)\n             minl=min(minl,pref[i]+suff[i]);\n        }\n        return minl==INT_MAX?-1:minl;\n    }\n};\n```
['Sliding Window', 'Prefix Sum']