[C++] concept and easy solution with dp
Although I could solve this during contest due to one edge case but I think is the shortest understandable approach.\n\nApproach is this:\n- Keep track of minim
Although I could solve this during contest due to one edge case but I think is the shortest understandable approach.\n\nApproach is this:\n- Keep track of minimum length subarray at each index of the nums\n- Whenever you find next subarray with sum=target just check if this subarry length plus any previous subarray length can be are our desired result(less than any two subarray sum untill this)\n\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();        \n        int res=INT_MAX;       \n        int i=0,j=0,sum=0;\n        vector<int> dp(n+1,INT_MAX);  // Just to keep track of minimum subarray untill current index\n        sum+=arr[0];\n        j=1;\n        while(j<=n){         \n            \n \xA0 \xA0 \xA0 \xA0 \xA0 \xA0while(i<j && sum>target){  // Check current windows if sum is greater than target\n                sum -= arr[i];   //shrink sliding windows\n                i++;\n            }\n            \n            if(target==sum){    // If current windows sum is target\n \xA0 \xA0 \xA0 \xA0 \xA0 \xA0 \xA0 \xA0if(dp[i]!=INT_MAX){ \xA0  // If  this is atleast 2nd subarray(we already found one subarray with target before)\n                    int curr = j-i+dp[i]; // length of this windows + length of previous minimum window \n                    res = min(res, curr);   // Check if these two subarrays are minimum length\n                }                       \n                dp[j] = min(j-i, dp[j-1]);  // check minimum subarray length with target at this index               \n            } else{            \n                dp[j] = dp[j-1];   // If current windows sum is not target and minimum subarray untill now is just like previous index\n            }        \n            \n            if(j==n) break;\n            \n            sum+=arr[j];\n            j++;\n        }       \n        if(res<=100000)   \n            return res;\n        return -1;        \n    }\n};\n```
[]