[CPP] Prefix Sum, Hash Map, Naive solution (EASY TO UNDERSTAND)
The following solution is a very easy and naive solution. Though I could not do it in the biweekly contest due to time constraint. I soon understood that prefix
The following solution is a very easy and naive solution. Though I could not do it in the biweekly contest due to time constraint. I soon understood that prefix sum is to be used.\n```\nclass Solution {\npublic:\n    int minSumOfLengths(vector<int>& arr, int target) {\n        int n = arr.size();\n        for(int i = 1;i<n;i++)  //Prefix Sum\n            arr[i]+=arr[i-1];\n        vector<pair<int,pair<int,int> > > interval;\n        unordered_map<int,int> mp;\n        for(int i = 0;i<n;i++)\n        {\n            if(arr[i] == target)    interval.push_back({i+1,{0,i}});\n            if(mp.find(arr[i]-target)!=mp.end())    interval.push_back({i-mp[arr[i]-target],{mp[arr[i]-target]+1,i}});\n\t\t\t\tmp[arr[i]] = i;\n        }\n        int ans = 0;\n        if(interval.size()<2)   return -1;\n        sort(interval.begin(),interval.end());\n\t\tans += interval[0].first;\n        int st = interval[0].second.first;  int en = interval[0].second.second;\n        for(int i = 1;i<interval.size();i++)   {\n            int st1 = interval[i].second.first; int en1 = interval[i].second.second;\n            if(st>st1 and st>en1)\n                return (ans+interval[i].first);\n            else if(st1>st and st1>en)\n                return (ans+interval[i].first);\n           \n        }\n        return -1;\n    }\n};\n```
['C', 'Sliding Window', 'Prefix Sum', 'C++']