BEATS 100 PERCENT CPP SOLUTIONS || VERY EASY TO UNDERSTAND
class Solution {\npublic:\n    int minSumOfLengths(vector& nums, int target) {\n        int pre =0;\n        int n=nums.size();\n        vector>st;\n        uno
class Solution {\npublic:\n    int minSumOfLengths(vector<int>& nums, int target) {\n        int pre =0;\n        int n=nums.size();\n        vector<pair<int,int>>st;\n        unordered_map<int,int>m;\n      \n        for(int i=0;i<n;i++){\n            pre+=nums[i];\n            if(pre==target){\n                 st.push_back({0,i});\n            }\n            if(m.find(pre-target)!=m.end()){\n                st.push_back({m[pre-target]+1,i});\n            }\n            m[pre]=i;\n        }\n        if(st.size()==0)return -1;\n        int res=1e9;\n        sort(st.begin(),st.end());\n        vector<int>mini(st.size(),-1);\n        mini[st.size()-1]=abs(st[st.size()-1].first-st[st.size()-1].second)+1;\n        for(int i=st.size()-2;i>=0;i--)\n        {\n            mini[i]=min(mini[i+1],abs(st[i].first-st[i].second)+1);\n        }\n       \n        for(int i =0;i<st.size()-1;i++){\n            int l=i+1;\n            int h =st.size()-1;\n            int pos =-1;\n            while(h>=l)\n            {\n                int mid=(l+h)/2;\n                if(st[mid].first>st[i].second){\n                    pos=mid;\n                    h=mid-1;\n                }\n                else{\n                    l=mid+1;\n                }\n            }\n            if(pos!=-1){\n                res=min(res,mini[pos]+abs(st[i].first-st[i].second)+1);\n            }\n        }\n        if(res==1e9)return -1;\n        return res;\n    }\n};
['Binary Tree']