Simple C++ Code
If you like the implementation then Please help me by increasing my reputation. By clicking the up arrow on the left of my image.\n\nclass Solution {\npublic:\n
# **If you like the implementation then Please help me by increasing my reputation. By clicking the up arrow on the left of my image.**\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();\n        int left[n], right[n];\n        unordered_map<int, int> lft;\n        unordered_map<int, int> rht;\n        int mod = 1e8+1;\n        lft[0] = -1;\n        rht[0] = n;\n        int prfx = arr[0];\n        lft[prfx] = 0;\n        if(prfx == target)\n            left[0] = 1;\n        else\n            left[0] = mod;\n        for(int i = 1; i < n; i++)\n        {\n            prfx += arr[i];\n            if(lft.count(prfx-target))\n            {\n                left[i] = min(left[i-1],i-lft[prfx-target]);\n            }\n            else\n            {\n                left[i] = left[i-1];\n            }\n            lft[prfx] = i;\n        }\n        prfx = arr[n-1];\n        rht[prfx] = n-1;\n        if(prfx == target)\n            right[n-1] = 1;\n        else\n            right[n-1] = mod;\n        for(int i = n-2; i >= 0; i--)\n        {\n            prfx += arr[i];\n            if(rht.count(prfx-target))\n            {\n                right[i] = min(right[i+1],rht[prfx-target]-i);\n            }\n            else\n            {\n                right[i] = right[i+1];\n            }\n            rht[prfx] = i;\n        }\n        // for(int i = 0; i < n; i++)\n        // {\n        //     cout << left[i] << " ";\n        // }\n        int ans = mod;\n        for(int i = 1; i < n; i++)\n        {\n            int v;\n            if(left[i-1] == mod || right[i] == mod)\n                v = mod;\n            else\n            {\n                v = left[i-1]+right[i];\n            }\n            //cout << v << " ";\n            ans = min(v, ans);\n        }\n        if(ans == mod) return -1;\n        return ans;\n    }\n};\n```
['Sliding Window']