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.