Java > Logic-2 > makeBricks (CodingBat Solution)

Problem:

We want to make a row of bricks that is goal inches long. We have a number of small bricks (1 inch each) and big bricks (5 inches each). Return true if it is possible to make the goal by choosing from the given bricks. This is a little harder than it looks and can be done without any loops. See also: Introduction to MakeBricks

makeBricks(3, 1, 8) → true
makeBricks(3, 1, 9) → false
makeBricks(3, 2, 10) → true


Solution:

public boolean makeBricks(int small, int big, int goal) {
  int digit = goal % 10;
  if (goal > small + (big * 5))
   return false;
  
  if (digit < 5 && small < digit)
    return false;
  else if (digit > 5 && digit > small + 5)
    return false;
  else
    return true;
}


17 comments :

  1. public boolean makeBricks(int small, int big, int goal) {

    if((big*5)>goal) goal%=5; //Too many 'big' bricks.
    else goal-=(5*big); //If not too many big bricks, then sub. what you have.

    if(small>=goal) return true;

    return false;
    }

    ReplyDelete
  2. public boolean makeBricks(int small, int big, int goal) {
    if (goal/5<=big) return(goal%5<=small);
    else return (small>=(goal-big*5));
    }

    ReplyDelete
  3. public boolean makeBricks(int small, int big, int goal) {
    return (goal>(big*5) + small) ? false : (goal%5 <= small) ? true : false;
    }

    ReplyDelete
  4. return goal % 5 <= small && big * 5 + small >= goal;

    ReplyDelete
    Replies
    1. EGYESBLOG - What an intelligent logic. How can i work on my skills to be able to think like that?

      Delete
  5. if ((goal/5 >=big) &&(goal-big*5)<=small) return true;
    if ((goal/5 < big)&&(goal-goal/5*5<=small))return true;

    return false;

    ReplyDelete
  6. if(big>=goal/5){
    big=goal/5;
    }

    return (big*5)+small>=goal;

    ReplyDelete
  7. while (big*5>goal)
    big = big - 1;
    return (5*big + small) >= goal;

    ReplyDelete
  8. return (small+5*big>=goal&&small>=goal%5);

    ReplyDelete
  9. if(goal % 5 > small)
    {
    return false;
    }

    return small + (big * 5) >= goal;

    ReplyDelete
  10. public boolean makeBricks(int small, int big, int goal) {
    int val = 0;
    if(goal>=big*5){
    val = goal - big*5;
    }
    else if ((goal/5)<=big){
    val = goal- (goal/5)*5;
    }
    if(small>=val){
    return true;
    }
    else{
    return false;
    }
    }

    ReplyDelete
  11. public static boolean test(int small,int big,int goal)
    {
    int s = small*1 + big*5;

    if (s >= goal)
    {
    return true;
    }
    else
    {
    return false;
    }
    }

    ReplyDelete
  12. return (goal/5<=big&& goal%5<=small)||(goal-big*5>0&& goal-big*5<=small);

    ReplyDelete
  13. public boolean makeBricks(int small, int big, int goal) {
    return goal % 5 <= small && big * 5 + small >= goal;
    //return (small+5*big>=goal&&small>=goal%5);
    //return (goal/5<=big&& goal%5<=small)||(goal-big*5>0&& goal-big*5<=small);
    //return (small+5*big>=goal&&small>=goal%5);
    //return goal % 5 <= small && big * 5 + small >= goal;
    //return (goal>(big*5) + small) ? false : (goal%5 <= small) ? true : false;
    }

    ReplyDelete
  14. public boolean makeBricks(int small, int big, int goal) {
    int total = big*5 + small;
    int div = goal/5;
    int sub = goal - div*5;
    if(goal<=total){
    if(sub<=small){
    return true;
    }
    return false;
    }
    return false;
    }

    ReplyDelete
  15. public boolean makeBricks(int small, int big, int goal) {
    if(goal > big*5 + small){
    return false;
    } else if(goal % 5 > small){
    return false;
    } else
    return true;
    }

    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