CHEFNWORK - DOUBT

#include<bits/stdc++.h>
using namespace std;
#define fori(i,a,b) for(int i=a;i<b;i++)

int main(){
int t;
cin>>t;
while(t–){
int n,k,ans=0,temp=0;
cin>>n>>k;
int a;
fori(i,0,n){
cin>>a;
if(a>k) {ans = -2; break;}
temp += a;
if(temp>k) {ans += 1; temp = a;}
}
cout<<ans+1<<endl;
}
}

Here is my code, i tried for an hour yesterday but couldnt get a submission, it shows WA everytime. I have searched for everything but i am found no test cases where this fail. do help and thanks!

1 Like

I think it is because you are not taking complete input.

Whenever you will break the loop remaining input will be considered as input for next test case.

2 Likes

It’s because you’re breaking the loop while taking input.

You can just store the input in an array and access the elements from the array.

int main(){
int t;
cin>>t;
while(t–){
int n,k,ans=0,temp=0;
cin>>n>>k;
int arr[n];
fori(i,0,n){
cin>>arr[i];
}
fori(i,0,n){
int a=arr[i];
if(a>k) {ans = -2; break;}
temp += a;
if(temp>k) {ans += 1; temp = a;}
}
cout<<ans+1<<endl;
}
return 0;
}

2 Likes

I had done this exact same error yesterday which costed me about 10 mins. You’re not taking complete input.

Please format your codes and also, why have you marked this as editorial? There is a help tag for such queries, please refrain from using editorial tags in such posts

1 Like

sorry, the comment was meant for @pratt3000. replied to your post (@pracurser) by mistake sorry :sweat_smile:

1 Like