C++ || Perfect Solution 😎😎 || O(N) TC || DP
TC: O(3 * N)\nSC: O(N)\n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        \n\t// Approach : Contraction and expansio
TC: O(3 * N)\nSC: O(N)\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        \n\t// Approach : Contraction and expansion of subarray to find the subaaray equals to target\n\t// if at any index i , sum > target --> start contracting the subarray by remove the element from front from subarray unless sum<=target\t\n\t// else continue the expansion of subarray\n        int n=arr.size();\n        vector<int>prefix(n);\n        vector<int>suffix(n);\n        int sum=0;\n        int sz=1e9,j=-1;\n        for(int i=0;i<n;i++){\n            sum+=arr[i];\n            while(sum>target){\n                j++;\n                sum-=arr[j];\n            }\n            if(sum==target){\n                sz=min(sz,i-j);\n            }\n            prefix[i]= (sz>=1e9 ? -1 : sz);\n        }\n        sum=0;\n        sz=1e9;\n        j=n;\n        for(int i=n-1;i>=0;i--){\n            sum+=arr[i];\n            while(sum>target){\n                j--;\n                sum-=arr[j];\n            }\n            if(sum==target){\n                sz=min(sz,j-i);\n            }\n            suffix[i]= (sz>=1e9 ? -1 : sz);            \n        }\n        \n        int size=1e9;\n        for(int i=0;i<n;i++){\n            int left=prefix[i];\n            int right = (i+1>=n ? -1 : suffix[i+1]);\n             \n            if(left!=-1 && right !=-1)\n                size = min(size, left+right);\n        }\n        return size>=1e9 ? -1 : size;\n    }\n};\n```
['Dynamic Programming', 'C++']