CodeChef CHFICRM Solution

Solution:
#include <stdio.h>

int main() {

    int n,i,j,k,a,b,c,dec;
    scanf("%d", &n);
    for(i=0;i<n;i++){
        a=0,b=0,c=0,dec=0;
        scanf("%d", &j);
        int x[j];
        for(k=0;k<j;k++){
            scanf("%d", &x[k]);
            if(x[k]==5){
                a++;
            }
            else if(x[k]==10){
                b++;
                a--;
            }
            else{
                c++;
                if(b>0)b--;
                else a-=2;
            }

            if(a<0 || b<0|| c<0) dec=1;
        }
        if(dec==1)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}


Solution with notes:

#include <stdio.h>

int main() {

    int n,i,j,k,a,b,c,dec;
    scanf("%d", &n);          //Number of test cases=n.
    for(i=0;i<n;i++){
        a=0,b=0,c=0,dec=0;    //Number of Rs.5 coin=a. No. of Rs.10 coin=b. No. of Rs.15 coin=c.                                                         Initially, the chef has no coin. if dec=0, that means he can sell icecream.  

        scanf("%d", &j);            //Number of person on que=j.
        int x[j];
        for(k=0;k<j;k++){          //Takes money from j people one by one.
            scanf("%d", &x[k]);

 //If Rs.5 is given, 'a' will increase by one as he will have one Rs.5 coin added to his cash.
            if(x[k]==5){     
                    a++;          
            }

//If Rs.10 is given, 'b' will increase by one as he will have one Rs.10 coin added to his cash. But 'a' will decrease bu one as he will have to return Rs.5 to the customer.
            else if(x[k]==10){     
                     b++;   
                     a--;    
            }

//If Rs.15 is given, 'c' will increase by one as he will have one Rs.15 coin added to his cash. But 'b' will decrease by one(b--) as he will have to returnRs.10 to the customer. But if he doesn't have Rs.10 coin(b<0), he will return 2 Rs.5 coins. Thus 'a' will decrease by 2(a-=2 or a=a-2).
            else{   
            c++;                        
            if(b>0)b--;
            else a-=2;
            }

            if(a<0 || b<0|| c<0) dec=1; //If chef is short of any coin, i.e(a<0 || b<0|| c<0); 'dec' will set it's 
        }                                               value to 1 (dec=1), declaring chef's inability to return money.
        if(dec==1)printf("NO\n");
        else printf("YES\n");
    }
    return 0;
}

Comments

Popular posts from this blog

Inspirational quotes