[C++]  Easy understand, intuitive O(n) [commented]
\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& a, int target) {\n        int n = a.size();\n        vector<int> s(n+1);\n        for (int i =
```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& a, int target) {\n        int n = a.size();\n        vector<int> s(n+1);\n        for (int i = 1; i <= n; ++i) s[i] = s[i-1]+a[i-1];\n        \n        unordered_map<int, int> m; // map[x,y] that holds sum \'x\' is available at index \'y\'\n        m[0] = 0; \n        const int INF = 1e9;\n        vector<int> x(n+1, INF), y(n+1, INF);\n        for (int i = 1; i <= n; ++i)\n        {\n            if (m.count(s[i]-target))\n            {\n                int tmp = m[s[i]-target]+1;\n                x[i] = i-tmp+1; // i is ending index\n                y[tmp] = i-tmp+1; // tmp is starting index\n            }\n            m[s[i]] = i;\n        }\n        \n        int ans = INF;\n        for (int i = 1; i < n; ++i)\n        {\n            x[i] = min(x[i], x[i-1]); // min length subarray ending in index i\n            ans = min(ans, x[i]+y[i+1]); // y[i+1] represents subarray starting from i+1 so that there is no overlap\n        }\n        if (ans == INF) ans = -1;\n        return ans;\n    }\n};\n```\nUpvote only if it helped, thanks.
[]