For those who would like to use set

google
Tags: #<Tag:0x00007f18258bc8f8>

#1

Comment int Solution::solve(vector &a, int b) {
multiset s;
int st=0,i=0,maxi,mini,siz=0;
while(i<a.size())
{//cout<<st<<" “<<i<<” “<<maxi<<” “<<mini<<” “<<(int)s.size()<<endl;
if(s.empty())
{s.insert(a[i]);
maxi=mini=(*s.begin());
siz=max(siz,(int)s.size());
i++;
}
else if(maxi-mini<b)
{
siz=max(siz,(int)s.size());
s.insert(a[i]);
maxi=(*s.rbegin());
mini=(*s.begin());
i++;
}
else
{auto it=s.find(a[st]);
//cout<<i<<” “<<st<<” ok\n";
s.erase(it);
if(!s.empty())
{maxi=(*s.rbegin());
mini=(*s.begin());}
st++;

}

}
if(maxi-mini<b)
siz=max(siz,(int)s.size());
return siz;}