- Iam getting error as TLE due to complexity O(n logn) whereas given Qs needs to be solved in O(n).
- Need to find alternative of m.find() method as it is taking O(logn) complexity and total complexity becomes O(nlog n).
vector<int> subarraySum(int a[], int n, long long s)
{
vector<int> result;
map<int,int>m;
map<int,int>::iterator it;
int b[n];
b[0]=a[0];
for(int i=1;i<n;i++){
b[i]=b[i-1]+a[i];
}
for(int i=0;i<n;i++){
m.insert({ b[i],i });
}
for(it=m.begin();it!=m.end();it++){
if((it->first-s)==0){
result.push_back(1);
result.push_back((it->second)+1);
break;
}
if(m.find(it->first-s)!=m.end()){
result.push_back(m[it->first-s]+2);
result.push_back((it->second)+1);
break;
}
}
if(result.empty())
result.push_back(-1);
return result;
}