Why is this Runtime and this AC

In today’s Code Ensemble, in the probem Cherry and bits, why is this code AC and this code getting RE. The only difference is the size of brr matrix which is 1005 in AC and 1001 in RE. As the constraints for n<=1000 and m<=1000, I think this should work.

AC
#include<bits/stdc++.h>
#define ll long long int
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
using namespace std; 
ll brr[1005][1005];
void solve(){
	  //input
	  ll n,m;
	  cin>>n>>m;
	  char arr[n+1][m+1];
	  ll i,j;
	  for(i=1;i<=n;i++){
		  for(j=1;j<=m;j++){
			  cin>>arr[i][j];
		  }
	  }
	  ll q;
	  cin>>q;
	  while(q--){
		  ll x1,y1,x2,y2;
		  cin>>x1>>y1>>x2>>y2;
		  brr[x1][y1]++;
		  brr[x2+1][y2+1]++;
		  brr[x1][y2+1]--;
		  brr[x2+1][y1]--;
	  }
		
		for(i=1;i<=n;i++){
			for (j=1;j<=m; j++) {
				brr[i][j]+=brr[i-1][j]+brr[i][j-1]-brr[i-1][j-1];
				if(brr[i][j]%2==1){
					if(arr[i][j]=='0'){
						arr[i][j]='1';
					}
					else{
						arr[i][j]='0';
					}
				}
			}
		}		
		for(i=1;i<=n;i++){
			for(j=1;j<=m;j++){
				cout<<arr[i][j];
		    }
		    cout<<endl;
		 }
 }     
int32_t main()
{	
     fio;
   ll t=1;
   //cin>>t;
   while(t--){
   	  solve();
   }
}
RTE
#include<bits/stdc++.h>
#define ll long long int
#define fio ios_base::sync_with_stdio(false); cin.tie(NULL);
#define endl "\n"
using namespace std; 
ll brr[1001][1001];
void solve(){
	  //input
	  ll n,m;
	  cin>>n>>m;
	  char arr[n+1][m+1];
	  ll i,j;
	  for(i=1;i<=n;i++){
		  for(j=1;j<=m;j++){
			  cin>>arr[i][j];
		  }
	  }
	  ll q;
	  cin>>q;
	  while(q--){
		  ll x1,y1,x2,y2;
		  cin>>x1>>y1>>x2>>y2;
		  brr[x1][y1]++;
		  brr[x2+1][y2+1]++;
		  brr[x1][y2+1]--;
		  brr[x2+1][y1]--;
	  }
		
		for(i=1;i<=n;i++){
			for (j=1;j<=m; j++) {
				brr[i][j]+=brr[i-1][j]+brr[i][j-1]-brr[i-1][j-1];
				if(brr[i][j]%2==1){
					if(arr[i][j]=='0'){
						arr[i][j]='1';
					}
					else{
						arr[i][j]='0';
					}
				}
			}
		}		
		for(i=1;i<=n;i++){
			for(j=1;j<=m;j++){
				cout<<arr[i][j];
		    }
		    cout<<endl;
		 }
 }     
int32_t main()
{	
     fio;
   ll t=1;
   while(t--){
   	  solve();
   }
}

Can anyone please tell, whats wrong in this

Did you calculate the maximum index you would ever try to access?

2 Likes

I think, As N and M<=1000, the maximum possible index I would access is A[1000][1000]. So A[1001][1001] should work I guess.

but read the code where you modify brr

2 Likes
          brr[x1][y1]++;
		  brr[x2+1][y2+1]++;
		  brr[x1][y2+1]--;
		  brr[x2+1][y1]--;

This? Oh okay, I am accessing upto brr[1001][1001]. Missed this silly thing. Thanks @galencolin

2 Likes