While and for loop not behivaing same

Getting different error with equivalent while and for loop, replacing while(q–) with for(int i=0;i<q;i++) in the exact code snippet gives error runtime error SIGXFSZ in while loop, whereas it works perfectly with for loop.

Code with for loop at line 19:


#include 
using namespace std;

int main() {
	// your code goes here
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t=0;
	cin>>t;
	int a[100000];
	int n,q;
	int k,l1,r1,l2,r2;
	while(t--)
	{
	    cin>>n>>q;
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	    }
	    for(int i=0;i<q;i++)
	    {
	        cin>>k>>l1>>r1>>l2>>r2;
	        int count=0;
	        for(int i=l1-1;i<r1;i++)
	        {
	            for(int j=l2-1;j<r2;j++)
	            {
	                if((a[i]^a[j]) & (1 << k))
	                {
	                    count++;
	                }
	            }
	        }
	        cout<<count<<'\n';
	    }
	}
	return 0;
}

Code with while loop at line 19:


#include 
using namespace std;

int main() {
	// your code goes here
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t=0;
	cin>>t;
	int a[100000];
	int n,q;
	int k,l1,r1,l2,r2;
	while(t--)
	{
	    cin>>n>>q;
	    for(int i=0;i<n;i++)
	    {
	        cin>>a[i];
	    }
	    while(q--)
	    {
	        cin>>k>>l1>>r1>>l2>>r2;
	        int count=0;
	        for(int i=l1-1;i<r1;i++)
	        {
	            for(int j=l2-1;j<r2;j++)
	            {
	                if((a[i]^a[j]) & (1 << k))
	                {
	                    count++;
	                }
	            }
	        }
	        cout<<count<<'\n';
	    }
	}
	return 0;
}

error with while loop

and with for loop i am getting wrong answer (only 1 image is allowed, so not attaching the screenshot here)

Please check the links for Code, html may change some code.

Another question is that why any of them is not have any TLE verdict ?

use long long int a[100000]; instead of int a[100000]; Both will then behave the same.
https://www.codechef.com/viewsolution/80282064
https://www.codechef.com/viewsolution/80282036
But still why while loop gives Runtime error is mysterious.

2 Likes

What is wrong with your while loop?

while(q--)
//is equal to:
while(q--!=0)

Why does this not work? Because you have an overflow with your q, causing you to run your while loop with a negative q. Since a negative q is not equal to 0, the while loop will keep running. At some point your q will cause yet another overflow, but this time the while loop will notice that, throwing an error.

The equivalent while loop to your for loop would be this:

while(q-->0)

(It is the loop I am using and the loop I would recommend. Yes, this loop is 2 characters longer, but it is more robust and slightly easier to understand)

2 Likes

But why using long long makes a differernce? as far as i know while has nothing to do with the variable a here.

but how q is overflowing? don’t while loop break immediately as q equals 0 before q has a negative value.

well, I can only guess since I do not want to debug your code.

But the max value of q is 50000, while the max value of int is at least 32767 in C++ . More here: Long Vs. Int C/C++ - What's The Point? - Stack Overflow

So maybe the overflow happens, because you try to read a q that is too big. Maybe you read the input wrong altogether. It really does not matter, bottom line: Don’t use int when using c++. You cannot guarantee relatively small numbers will fit.

Storing a too big number will cause an overflow, this happens in your code. In this case when reading a number and storing it inside q(int). This causes you to run the while loop with a negative q, which later throws an error.

1 Like