whats wrong with my code?

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.
valgrind complained about your array, try to declare it outside main.


> I don't know why its giving wrong answers in some of the test cases
explain your logic
@ne555 there is no problem with declaring that array, I have declared bigger array inside

My logic is as follows, at the time of input I count the number of continuous 1's and 0's and create a new array with there count, +ve count for 1's and -ve count for 0's
For eg, input array = 11100011011, my array will look like 3|-3|2|-1|2
now as the ques asks, to right shift the array, I update the array, if last element if +ve, i do -1 and if +1 in first element, and same for -ve
For cases where first index is +ve and last index is -ve, I increment the last element and decrement the first index and add a -1 there

For eg, entered array is 110100 and my array is 2|-1|1|-2
after right shift array will change 011010 and my array will also go -1|2|-1|1|-1

so, I think my logic is right but I'm missing something
You should know that the Codechef adjudicators are aware of this forum, and that people use it to cheat on their contests. If you use this forum to try and cheat, you run the risk of being disqualified.
Why don't you start with a simple solution first, like use std::deque to store all the digits. You could easily rotate the them by removing from the back and inserting in the front.
To get the longest contigous sequence write a simple algorithm first.
If this is not sufficient then look for a way to optimize it.
here's a testcase
5 11 5
1 1 0 1 1
?!?!?!?!?!?
2
3
4
4
3
2
however, your code keeps saying that the answer is 4
notice that the biggest stride of `1' was split, but you failed to update your maximum
Woah @ne555 thats what I was missing, thanks buddy
Topic archived. No new replies allowed.