https://www.codechef.com/NOV18B/problems/HMAPPY1
long int n,q,k,x,prev=-1,arr[600001]={0},last=400000,first,i;
string str;
cin>>n>>q>>k;
for(i=0;i<n;i++)
{
cin>>x;
if(x!=prev)last++;
if(x==1)arr[last]++;
else arr[last]--;
prev=x;
}
first=400001;
x=*max_element(arr+(first-1),arr+(last+1));
cin>>str;
for(i=0;i<q;i++)
{
if(str[i]=='?')
{
if(x<arr[first])
x=arr[first];
if(x>k)
x=k;
cout<<x<<endl;
}
else
{
if((arr[first]>0)&&(arr[last]>0))
{
arr[first]++;
arr[last]--;
}
else if((arr[first]<0)&&(arr[last]<0))
{
arr[first]--;
arr[last]++;
}
else if((arr[first]>0)&&(arr[last]<0))
{
arr[last]++;
first--;
arr[first]--;
}
else if((arr[first]<0)&&(arr[last]>0))
{
arr[last]--;
first--;
arr[first]++;
}
if(arr[last]==0)last--;
}
for above question, I'm using this program. I just count the numbe rof 1's and 0's at the entering time and store them in another array, by their count
For eg, input array = 11100011011, my array will look like 3|-3|2|-1|2 , positive for count of 1's and negative for 0's.
I've checked multiple times but I don't know why its giving wrong answers in some of the test cases, some of test cases are accepted.