Java > Logic-2 > makeChocolate (CodingBat Solution)

Problem:

We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.

makeChocolate(4, 1, 9) → 4
makeChocolate(4, 1, 10) → -1
makeChocolate(4, 1, 7) → 2


Solution:

public int makeChocolate(int small, int big, int goal) {
  int rem = goal % 5;
    
  if (small + (big*5) < goal)
    return -1;
  else if (rem <= small && goal - big*5 > 4)
    return rem + 5;
  else if (rem <= small)
    return rem;
  else
    return -1;
  
  
  

}


22 comments :

  1. public int makeChocolate(int small, int big, int goal) {

    int a = (goal-5*big);
    if (a<=small && a>=0) return a;
    if (a<0 && goal%5<=small) return goal%5;
    return -1;

    }

    ReplyDelete
  2. public int makeChocolate(int small, int big, int goal) {
       if (big * 5 + small < goal | small < goal % 5) return -1;
       if (big * 5 <= goal) return goal - big * 5;
       return goal % 5;
    }

    ReplyDelete
  3. public int makeChocolate(int small, int big, int goal) {
    if ((small + big * 5) < goal) return -1;
    int smallPacks = goal - (Math.min(goal / 5, big) * 5);
    return smallPacks > small ? -1 : smallPacks;
    }

    ReplyDelete
  4. public int makeChocolate(int small, int big, int goal) {
    int bigReq = goal / 5;
    if (big <= bigReq)
    goal -= big * 5;
    else
    goal -= bigReq * 5;
    return small >= goal ? goal : -1;
    }

    ReplyDelete
  5. public int makeChocolate(int small, int big, int goal) {
    int big_t = big * 5;

    if (goal > big_t + small) {
    return -1;
    } else if (goal / 5 > big && goal % 5 > small) {
    return -1;
    } else if ((goal - (goal / 5 * 5)) > small) {
    return -1;
    } else {
    if (goal / 5 > big) {
    return goal - big_t;
    } else {
    return goal - (goal / 5 * 5);
    }
    }

    }

    ReplyDelete
  6. int ones = (goal/5>=big&&(goal-big*5)<=small)?
    (goal-big*5):
    (goal/5<big&&goal%5<=small)? (goal%5) : -1;
    return ones;

    ReplyDelete
  7. public int makeChocolate(int small, int big, int goal) {

    int numBig = big;
    while (numBig*5 > goal){
    numBig--;
    }

    if (numBig > big){
    numBig = big;
    }

    int rem = goal - (numBig*5);

    if (small >=rem){
    return rem;
    }

    return -1;
    }

    ReplyDelete
    Replies
    1. correction. superfluous code removed:

      public int makeChocolate(int small, int big, int goal) {

      int numBig = big;
      while (numBig*5 > goal){
      numBig--;
      }

      int rem = goal - (numBig*5);

      if (small >=rem){
      return rem;
      }

      return -1;
      }

      Delete
  8. public int makeChocolate(int small, int big, int goal) {

    int i=goal%5;
    int j=goal/5;
    int k=(big*5)-goal;
    if((big*5)+small<goal)return -1;
    if((j<=big)&&(i<=small))return i;
    if((big*5)<goal)
    {
    if(Math.abs(big*5-goal)<=small)return Math.abs(big*5-goal);
    }
    return -1;
    }

    ReplyDelete
  9. public int makeChocolate(int small, int big, int goal) {
    int big_t = big * 5;

    if (goal > big_t + small) {
    return -1;
    } else if (goal / 5 > big && goal % 5 > small) {
    return -1;
    } else if ((goal - (goal / 5 * 5)) > small) {
    return -1;
    } else {
    if (goal / 5 > big) {
    return goal - big_t;
    } else {
    return goal - (goal / 5 * 5);
    }
    }

    }

    ReplyDelete
  10. public int makeChocolate(int small, int big, int goal) {
    if (0 <= (goal - 5 * big) && (goal - 5 * big) <= small){
    return (goal - 5 * big);
    }
    if ((goal - 5 * big) < 0 && (goal % 5) <= small){
    return (goal % 5);
    }
    return -1;
    }

    ReplyDelete
  11. With above code, it will fail for this testcase makeChocolate(60, 100, 550) → 50

    Little modification to above method
    public int makeChocolate(int small, int big, int goal) {
    int rem = goal%5;
    if(goal > small+big*5)
    return -1;
    else if (rem <= small && goal - big*5 > 4)
    return goal - big*5;
    else if(rem<=small)
    return rem;
    else
    return -1;
    }

    ReplyDelete
  12. public int makeChocolate(int small, int big, int goal) {

    return(goal-5*big>small)?-1:(goal-5*big==0)?0:(goal-5*big>0)?goal-5*big:(goal%5<=small)?goal%5:-1;

    }

    ReplyDelete
  13. public int makeChocolate(int small, int big, int goal) {
    int large = big * 5;

    if (goal >= 10) {
    int w = goal - large;
    if (w <= small) return w;
    else return -1;
    }
    if (goal % 5 <= small) return goal % 5;

    return -1;
    }

    ReplyDelete
  14. public int makeChocolate(int small, int big, int goal) {
    int count=goal%5;
    if(small+big*5big*5 && goal%5==0){
    return goal-big*5;
    }
    else if(count<=small && goal-big*5>4) return count+5;
    else if(count<=small) return count;
    return -1;

    }

    ReplyDelete
  15. int res=0;
    int i=0;
    if(goal>big*5+small)
    return -1;
    while(res<=goal && igoal) res=res-5;
    if(goal-res>small)return-1;
    return(goal-res);

    ReplyDelete
  16. if (small + big * 5 < goal) return -1;
    int numBig = (big * 5 > goal) ? ((int) Math.floor(goal/5)) : big;
    if (goal - numBig*5 <= small) return goal - numBig*5;
    return -1 ;

    ReplyDelete
  17. public int makeChocolate(int small, int big, int goal) {
    if (small + 5 * big < goal) {
    return -1;
    } else if (5 * big < goal && goal-5*big <= small) {
    return goal-5*big;
    } else if (5 * big > goal && goal%5 <= small) {
    return goal%5;
    } else if (5 * big == goal) {
    return 0;
    } else {
    return -1;
    }
    }

    ReplyDelete
  18. public int makeChocolate(int small, int big, int goal) {
    if(big*5==goal) return 0;
    for(int i =0; i<=big ; i++){
    for (int j =0; j <= small ; j++){
    if ((i*5) == goal){
    return 0;}

    else if(j>5){
    if ((big*5)+j == goal){
    return j;
    }
    }
    else if(((i*5)+j)==goal){
    return j;}
    }
    }
    return -1;

    }

    ReplyDelete
  19. public int makeChocolate(int small, int big, int goal) {
    int bigTotal = big * 5;
    while (bigTotal > goal) {
    bigTotal -= 5;
    }
    int amount = goal - bigTotal;
    if (amount <= small) {
    return amount;
    }
    else {
    return -1;
    }
    }

    ReplyDelete
  20. public int makeChocolate(int small, int big, int goal) {
    if((big*5)+small < goal)
    return -1;
    if(goal%5 > small)
    return -1;
    if(big*5>goal)
    return goal%5;
    else return goal - (big*5);
    }

    ReplyDelete
  21. public int makeChocolate(int small, int big, int goal) {
    for (int i = 0; i <= big; i++) {
    boolean isValid = (i * 5 == goal);

    if (i * 5 > goal) {
    big = i - 1;
    }

    if (isValid) return 0;
    }

    for (int i = 0; i <= small; i++) {
    if ((big * 5) + i == goal) {
    return i;
    }
    }
    return -1;
    }

    ReplyDelete

Follow Me

If you like our content, feel free to follow me to stay updated.

Subscribe

Enter your email address:

We hate spam as much as you do.

Upload Material

Got an exam, project, tutorial video, exercise, solutions, unsolved problem, question, solution manual? We are open to any coding material. Why not upload?

Upload

Copyright © 2012 - 2014 Java Problems  --  About  --  Attribution  --  Privacy Policy  --  Terms of Use  --  Contact