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

Subtasks

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

Subtask #2 (60 points): original constraints

Sample Input 1 

3
2
5 10
2
10 5
2
5 15

Sample Output 1 

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
	{
		// your code goes here
		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() {
	// your code goes here
	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.

Leave a Comment