Easy C++ solution( By using Kadane's algorithm)


#1

vector Solution::flip(string A)
{
vector< int>v;
int count=0,max_count=INT_MIN,i,l,r,start=0,c=0;
for(i=0;i<A.length();i++)
{
if(A[i]==‘1’)
{
c++;
count–;
}
else if(A[i]==‘0’)
{
count++;
}
if(count>max_count)
{
max_count=count;
l=start;
r=i;
}
if(count<0)
{
count=0;
start=i+1;
}
}
if(c==A.length())
{
return {};
}
else
{
v.push_back(l+1);
v.push_back(r+1);
return v;
}
}