sliding window + dp
\n/*\n\n    dp[i] : the length of the shortest subarray which lies in the range [0...i], having a sum = target\n    we can use sliding window to solve this prob
```\n/*\n\n    dp[i] : the length of the shortest subarray which lies in the range [0...i], having a sum = target\n    we can use sliding window to solve this problem\n\n    as soon as [i...j] has a sum = target, the second subarray will be i...j, and we will take the first subarray\n    given by dp[i - 1]\n\n*/\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();\n        vector<int> dp(n, 1e7);\n        int ans = INT_MAX;\n\n\n        int i = 0, j = -1;\n        int sum = 0;\n        int res = 1e7;\n\n        while (j < n - 1) {\n            sum += arr[++j];\n            while (i <= j && sum > target) {\n                sum -= arr[i++];\n            }\n\n            if (sum == target) {\n                dp[j] = min(dp[j], j - i + 1);\n\n                if (i > 0) ans = min(ans, j - i + 1 + dp[i - 1]);\n            }\n            if (j > 0) dp[j] = min(dp[j], dp[j - 1]);\n        }\n\n\n        return ans > 1e5 ? -1 : ans;\n    }\n};\n```
['Dynamic Programming', 'Sliding Window']