< Back to forum

Chef Feeds Cats

#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.


Enter your answer details below:


Enter your comment details below:




2 Answer(s)

avatar

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

viking_prime last updated on March 28, 2020, 10:06 p.m. 0    Reply    Upvote   

avatar

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 using namespace std;

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.

avatar

NONE

hritesh0027 last updated on March 28, 2020, 10:36 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.