My issue
include
using namespace std;
void diet(){
int N,K;
cin>>N>>K;
int P=0;
for(int i=1;i<=N;i++){
int A;
cin>>A;
P=P+A;
if(P>=K) P=P-K;
else{
cout<<“NO”<<" "<<i<<endl;
break;
}
if(i==N) cout<<“YES”<<endl;
}
}
int main() {
// your code goes here
int t;
cin>>t;
while(t–){
diet();
}
return 0;
}
i dont know why in the third test case its printing the wrong i value ,if we check it should print i as 3,can you check where it went wrong?
My code
#include <iostream>
using namespace std;
void diet(){
int N,K;
cin>>N>>K;
int P=0;
for(int i=1;i<=N;i++){
int A;
cin>>A;
P=P+A;
if(P>=K) P=P-K;
else{
cout<<"NO"<<" "<<i<<endl;
break;
}
if(i==N) cout<<"YES"<<endl;
}
}
int main() {
// your code goes here
int t;
cin>>t;
while(t--){
diet();
}
return 0;
}
Learning course: 1000 to 1400 difficulty problems
Problem Link: Chef Diet Practice Problem in - CodeChef