O(N) & O(N^2), window sliding, simple approach, w/ video explanation of subarray sum.
UPDATE \nO(N) approach is based on this beautiful solution here,\nhttps://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss
**UPDATE** \nO(N) approach is based on this beautiful solution here,\nhttps://leetcode.com/problems/find-two-non-overlapping-sub-arrays-each-with-target-sum/discuss/685486/JAVA-O(N)-Time-Two-Pass-Solution-using-HashMap by @pramitb\n\n**O(N^2) Approach**\n\n\nI am using simple approach from the following video, where I have explained how can we find a subarray whose sum is equal to a given target, \nIn this case I am just storing length of each such subarray, start and end index in a vector. \nAnd then sort this vector by length, \n\n\nhttps://www.youtube.com/watch?v=1XLTftBtqZs\n\n\n```\n// using subarray sum equals to target, \n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        \n        int n = arr.size();\n        map<int, int> _map; // can use unordered_map as well\n        vector<pair<int, pair<int, int> > > subs;\n        \n        _map[0] = -1;\n        int sum = 0;\n        \n        for(int i = 0; i < n; i++) {\n            sum += arr[i];\n            \n            if(sum - target >= 0 && _map.find(sum - target) != _map.end()) {\n                subs.push_back({i - _map[sum-target], {_map[sum - target] + 1, i}});\n            }\n            _map[sum] = i;\n        }\n        \n        sort(subs.begin(), subs.end());\n        if(subs.size() < 2)\n            return -1;\n        \n        \n        for(int i = 0; i < subs.size() - 1; i++) {\n            \n            for(int j = i + 1; j < subs.size(); j++) {\n                if(subs[i].second.second < subs[j].second.first || subs[i].second.first > subs[j].second.second)\n                    return subs[i].first + subs[j].first; \n            }\n        }\n        return -1;\n    }\n};\n```\n\n\n**O(N)** ( thanks to @pramitb for his solution)\n\nBetter approach is that we can compute and store all the prefix sum. And then traverse the array again and restart computing the prefix sum ( i.e  `sum = 0`). And this time for each `i` we also check if there is a subarray whose sum is equals to `sum + target` , if we find such value in map that means there exist a subarray on the right of current whose sum will be equals to `target`. And then we can compare both of them, and store the minimum answer. Check below the code for better understanding.\n\n\n```\n\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        \n        int n = arr.size();\n        map<int, int> _map;\n        \n        _map[0] = -1;\n        int sum = 0;\n        \n        for(int i = 0; i < n; i++) {\n            sum += arr[i];\n            _map[sum] = i;\n        }\n       \n        sum = 0;\n        \n        int ans = INT_MAX;\n        int min_left = INT_MAX;\n        \n        for(int i = 0; i < n; i++){\n            sum += arr[i];\n            \n            if(_map.find(sum - target) != _map.end()){\n                min_left = min(min_left, i - _map[sum - target]); \n            }\n\t\t\t\n            if( _map.find(sum + target) != _map.end() && min_left < INT_MAX){\n                ans = min(ans, _map[sum + target] - i + min_left);\n            }\n        }\n        return ans == INT_MAX ? -1 : ans;\n    }\n};\n```\n\n\n
['Sliding Window']