#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.
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!)
Your program fails on the sample test cases . kindly check your program .
See where you are using break and how input is working after that
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 .
the K is not inputted correctly in t=3,of sample test case ,
what is wrong
See which value k has taken and try to find it. The reason has already been told
ohh nice didnt think about it thanks a lot !
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 .