#include
using namespace std; int main() { int t,j; cin>>t; for(j=0;j<t;j++) { int n, m ,q, r ,flag1=1 ,flag2=1 ,k ,l ,sum1=0, sum2; cin>>n>>m; q=m/n; int j; j=qn; int arr[j]; sum2=(n(n+1))/2; for(k=0;k<j; k++) { arr[k]=0; } for(k=0;k<q ;k++) {
for(l=n*k ;l<n*k+n ;l++) { cin>>arr[l]; sum1=sum1+arr[l]; } if(sum1!=sum2) { flag1=0; break; } sum1=0; } r=m%n; if(r) { int arr2[r],hash[n]; for(k=0;k<n ;k++) { hash[k]=0; } for(k=0;k<r ;k++) { arr2[k]=0; } for(k=0;k<r ;k++) { cin>>arr2[k]; hash[arr2[k]]++; if(hash[arr2[k]]>1) { flag2=0; break; } } if(flag1==1 && flag2 ==1) { cout<<"YES"<<endl; } else { cout<<"NO"<<endl; } } } return 0;
}
Asked by: Anonymous on March 27, 2020, 9:45 a.m. Last updated on March 27, 2020, 9:45 a.m.
You have defined j twice, once before testcases and once inside it.
Also see to it that the array might be of size 0 if q is 0 as there is no guarantee that m>n
Also I would suggest taking the inputs first and then trying your calculations because there might be some problem there
i have. also add cond if(q!=0) { ..... } and made changes in j then also it is showing runtime error. so pl check whats the problem now.and thank you for your interest. on running printf(cout) i found there is some error in "NO" one but not able to find it.
code
#include
int main() { int t,j; cin>>t; for(j=0;j<t;j++) { int n,m,q,r,parth=1,khushi=1,k,l,sum1=0,sum2=0; cin>>n>>m; q=m/n; cout<<q<<" "; if(q!=0) { int p; p=qn; int arr[p]; sum2=(n(n+1))/2;
for(k=0;k<p;k++)
{
arr[k]=0;
}
for(k=0;k<q;k++)
{
for(l=n*k;l<n*k+n;l++)
{
cin>>arr[l];
sum1=sum1+arr[l];
cout<<sum1<<" ";
}
cout<<endl;
if(sum1!=sum2)
{
parth=0;
break;
}
sum1=0;
}
}
r=m%n;
if(r!=0)
{
int arr2[r],hash[n+1];
hash[0]=0;
for(k=1;k<=n;k++)
{
hash[k]=0;
}
for(k=0;k<r;k++)
{
arr2[k]=0;
}
for(k=0;k<r;k++)
{
cin>>arr2[k];
hash[arr2[k]]++;
}
for(k=0;k<r;k++)
{
if(hash[arr2[k]]>1)
{
khushi=0;
break;
}
}
}
if(parth==1 && khushi ==1)
{
cout<<"YES"<<" "<<parth<<" "<<khushi<<endl;
}
else
{
cout<<"NO"<<" "<<parth<<" "<<khushi<<endl;
parth=1;
khushi=1;
}
}
return 0;
}
parthvyasvadodara last updated on March 31, 2020, 1:05 p.m.