Why only subtask 1 got passed in Icecream question

Please look at my code for Chef and icecream question, which passed for subtask 2 but failed for subtask 1:

#include <bits/stdc++.h>
using namespace std;

int main() {
	int t;
	cin>>t;
	while(t--){
	   int n;
	   cin>>n;
	   int cost[n];
	   
	   for(int i=0;i<n;i++){
	       cin>>cost[i];
	   }
	   int leftMoney=0,i=0,flag=0;
	   while(i<n){
	       if((cost[i]-5)>leftMoney){
	           flag=1;
	           break;
	       }
	       leftMoney-=cost[i]-5;
	       leftMoney+=5; //price of an icecream added
	       i++;
	   }
	   if(flag==0) //all request satisfied
	    cout<<"YES"<<endl;
	   else
	    cout<<"NO"<<endl;
	}
	return 0;
}

You need to change leftmoney += 5 to leftmoney += cost[i]
If you only add 5, then even if someone give 10 rupee coin then also your code will add 5.
So your code failed on this test
3
5 10 15
It’s answer is YES, and your code gives NO.

all cases got failed when i did this…(just now)

Sryy i didn’t added the tracking part you also need to keep the track of individual coins in our case only keeping the track of 5, 10 coin is sufficient.
See in previous modification there was no data on what coin does chef has. A case will appear when you have leftmoney >= 10 and your customer gives you $10 coin. So if you don’t have any $5 coin then answer should be NO but during previous modification it will say YES.
But on the other if you do have 2 coins of 5 you can give change back to $15 coustmer.
This is what i implemented my code

1 Like