# Chef and Icecream Codechef Solution

Chef and Icecream Codechef Solution: Chef owns an icecream shop in Chefland named scoORZ. There are only three types of coins in Chefland: Rs. 5, Rs. 10 and Rs. 15. An icecream costs Rs. 5.

There areÂ NNÂ people (numberedÂ 11Â throughÂ NN) standing in a queue to buy icecream from scoORZ. Each person wants to buy exactly one icecream. For each validÂ ii, theÂ ii-th person has one coin with valueÂ aiai. It is only possible for someone to buy an icecream when Chef can give them back their change exactly â€• for example, if someone pays with a Rs. 10 coin, Chef needs to have a Rs. 5 coin that he gives to this person as change.

Initially, Chef has no money. He wants to know if he can sell icecream to everyone in the queue, in the given order. Since he is busy eating his own icecream, can you tell him if he can serve all these people?

### Input

• The first line of the input contains a single integerÂ TTÂ denoting the number of test cases. The description ofÂ TTÂ test cases follows.
• The first line of each test case contains a single integerÂ NN.
• The second line containsÂ NNÂ space-separated integersÂ a1,a2,â€¦,aNa1,a2,â€¦,aN.

### Output

For each test case, print a single line containing the stringÂ `"YES"`Â if all people can be served orÂ `"NO"`Â otherwise (without quotes).

### Constraints

• 1â‰¤Tâ‰¤1001â‰¤Tâ‰¤100
• 1â‰¤Nâ‰¤1031â‰¤Nâ‰¤103
• aiâˆˆ{5,10,15}aiâˆˆ{5,10,15}Â for each validÂ ii

Subtask #1 (40 points):Â aiâˆˆ{5,10}aiâˆˆ{5,10}Â for each validÂ ii

Subtask #2 (60 points):Â original constraints

```3
2
5 10
2
10 5
2
5 15
```

```YES
NO
NO
```

### Explanation

Example case 1:Â The first person pays with a Rs. 5 coin. The second person pays with a Rs. 10 coin and Chef gives them back the Rs. 5 coin (which he got from the first person) as change.

Example case 2:Â The first person already cannot buy an icecream because Chef cannot give them back Rs. 5.

Example case 3:Â The first person pays with a Rs. 5 coin. The second person cannot buy the icecream because Chef has only one Rs. 5 coin, but he needs to give a total of Rs. 10 back as change.

## Chef and Icecream â€“ CodeChef Solution in JAVA

```import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
public static void main (String[] args) throws java.lang.Exception
{
Scanner sc=new Scanner(System.in);
int tc=sc.nextInt();
int i,checker,count5,count10;
for(int it=1;it<=tc;it++){
checker=1;
count5=0;
int size=sc.nextInt();
count10=0;
int[] arr=new int[size];
for(i=0;i<size;i++){
arr[i]=sc.nextInt();
}
if(arr[0]==10||arr[0]==15){
System.out.println("NO");
}else{
for(i=0;i<size;i++){
if(arr[i]==5){
count5++;
}
if(arr[i]==10){
count10++;
count5--;
}
if(arr[i]==15){
if(count10>=1){
count10--;
}else{
count5-=2;
}
}
if(count10<0||count5<0){
checker=0;
break;
}
}
if(checker==0){
System.out.println("NO");
}else{
System.out.println("YES");
}
}
}
}
}
```

## Chef and Icecream- CodeChef Solution in CPP

```#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin>>t;
while(t--)
{
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++) cin>>a[i];
int flag=0,cnt1=0,cnt2=0;
for(int i=0;i<n;i++)
{
if(a[i] ==5) cnt1++;
else if(a[i]==10 and cnt1>0){
cnt2++; cnt1--;
}
else if(a[i]==15 and (cnt1>1 or cnt2>0)) {
if(cnt2>0) cnt2--;
else cnt1-=2;
}
else {
flag=1;
break;
}
}
if(flag==0) cout<<"YES\n";
else cout<<"NO\n";
}
return 0;
}
```

## Chef and Icecream -CodeChef Solution in Python

```for _ in range(int(input())):
n=input()
a=list(map(int,input().split()))
b=[0,0,0]
for i in a:
if(i==5):
b[0]+=1
elif(i==10):
b[1]+=1
b[0]-=1
else:
b[2]+=1
if(b[1]==0):
b[0]-=2
else:
b[1]-=1
if(b[0]<0 or b[1]<0 or b[2]<0):
print('NO')
break
else:
print('YES')```

Disclaimer:Â The above Problem (Chef and Icecream) is generated byÂ CodeChef but the solution is provided byÂ Chase2learn.This tutorial is only forÂ EducationalÂ andÂ LearningÂ purpose.

Sharing Is Caring