WA in "Chef Diet" (DIET) - please help

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,k,sum=0;
        cin>>n>>k;
        int a[n];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            sum=sum+a[i];
            if(sum>=k)
            {
                sum=sum-k;
                
            }
            else 
            {sum=0;
                break;
            }
        }
        if(sum==0)
        {
            cout<<"NO "<<i+1<<endl;
        }
        else 
        {
            cout<<"YES"<<endl;
        }
    }
    return 0;
}

suppose n=2 k=3 and A1=3 and A2=3.
your code shows no because your code fails when he buys exactly the required amount of protein so instead of putting sum as 0 in else case try sum =-1;
keep up with the practise.

1 Like

i did that but still getting WA , where am I messing up ?

#include<iostream>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int i,n,k,sum=0;
        cin>>n>>k;
        int a[n];
        for(i=0;i<n;i++)
        {
            cin>>a[i];
            sum=sum+a[i];
            if(sum>=k)
            {
                sum=sum-k;
                
            }
            else 
            {sum=-1;
                break;
            }
        }
        if(sum==-1)
        {
            cout<<"NO "<<i+1<<endl;
        }
        else 
        {
            cout<<"YES"<<endl;
        }
    }
    return 0;
}

This fails on the sample test input.

Edit:

As a hint: add some debugging code that outputs K after reading it for each testcase and run your program with the sample test input (remember to remove this debugging code when you come to submit it, though!)

2 Likes

Your program fails on the sample test cases . kindly check your program .

1 Like

See where you are using break and how input is working after that

1 Like

yes but why i am not getting

ok

ok ii check

Because , you are using break inside for loop . so it cannot read remaining inputs if condition fails .

Hint : Use separate for loops to read and process the inputs .

2 Likes

the K is not inputted correctly in t=3,of sample test case ,
what is wrong

1 Like

See which value k has taken and try to find it. The reason has already been told

2 Likes

ohh nice didnt think about it thanks a lot !

1 Like

ok thanks !

If you get stuck in any place , kindly try to find using the cout statement .
So that you can easily find out the error .

1 Like