Have a small doubt ....please help

https://www.codechef.com/LICO2020/problems/ISHGRAPH

while trying this problem ,i faced a very strange doubt.

while declaring array x and y locally i am getting wrong answer ,but at the same time if i declare the arrays x and y globally i am getting correct answer…why is this happenning?
why such thing ?? please help

#include <bits/stdc++.h>
using namespace std;

int main() {
int x[1000010],y[1000010];
int b;
cin>>b;

while(b--)
{
    int xmin=1e7,xmax=0,ymin=1e7,ymax=0;
    for (int i=0;i<3;i++)
    {
        int xa,ya;
        cin>>xa>>ya;
        xmin=min(xmin,xa);
        xmax=max(xmax,xa);
        ymin=min(ymin,ya);
        ymax=max(ymax,ya);
    }
    xmin++;ymin++;
    x[xmin]++;
    y[ymin]++;
    x[xmax]--;
    y[ymax]--;
    
}
for (int i=1;i<1000010;i++)
{
    x[i]+=x[i-1];
    y[i]+=y[i-1];
}
int f;

cin>>f;
while(f--)
{
    char a;char b;int i;
    cin>>a>>b>>i;
    
    if (a=='x')
    {
        cout<<x[i]<<endl;
        
    }
    else
       cout<<y[i]<<endl;
    
}

return 0;

}

I think when you are declaring array locally then its size should be around 1e5. But in case of globally you can go upto 1e7. But in case of bool or some rare cases you can go upto 1e8 globally.

Globally you are allowed to declare an array of size 10^7, but locally you can declare array upto 10^6

I think its because your array size is large and we know that in global declaration we have more memory allocated than local declaration so it happened i guess

You should initialize the array with zeros like this

int x[1000010] = {0}, y[1000010] = {0};

if you are declaring locally.
In case of global declaration array already initialized with zeros that’s why were getting correct answer.

see the correct submission using local declaration link

1 Like

thanks a lot for this explaination…it helps me…