'Don't understand the logic behind the problem

i'm trying to understand what the problem with my code is. Here is the task and my code:

Write a method named canPack with three parameters of type int named bigCount, smallCount, and goal.

  • The parameter bigCount represents the count of big flour bags (5 kilos each).

  • The parameter smallCount represents the count of small flour bags (1 kilo each).

  • The parameter goal represents the goal amount of kilos of flour needed to assemble a package.

Therefore, the sum of the kilos of bigCount and smallCount must be at least equal to the value of goal. The method should return true if it is possible to make a package with goal kilos of flour.

If the sum is greater than goal, ensure that only full bags are used towards the goal amount. For example, if goal = 9, bigCount = 2, and smallCount = 0, the method should return false since each big bag is 5 kilos and cannot be divided. However, if goal = 9, bigCount = 1, and smallCount = 5, the method should return true because of 1 full bigCount bag and 4 full smallCount bags equal goal, and it's okay if there are additional bags left over.

If any of the parameters are negative, return false.

EXAMPLE INPUT/OUTPUT:

  • canPack (1, 0, 4); should return false since bigCount is 1 (big bag of 5 kilos) and goal is 4 kilos.

  • canPack (1, 0, 5); should return true since bigCount is 1 (big bag of 5 kilos) and goal is 5 kilos.

  • canPack (0, 5, 4); should return true since smallCount is 5 (small bags of 1 kilo) and goal is 4 kilos, and we have 1 bag left which is ok as mentioned above.

  • canPack (2, 2, 11); should return true since bigCount is 2 (big bags 5 kilos each) and smallCount is 2 (small bags of 1 kilo), makes in total 12 kilos and goal is 11 kilos.

  • canPack (-3, 2, 12); should return false since bigCount is negative.

    public static boolean canPack(int bigCount, int smallCount, int goal) {

        int bigCountKilos = bigCount * 5;
        int smallCountKilos = smallCount;

        if (bigCount < 0 || smallCount < 0 || goal < 0) {
            return false;
        } else {

            if (bigCountKilos >= goal && bigCountKilos % goal == 0) {
                return true;
            } else if (bigCountKilos + smallCountKilos >= goal && bigCountKilos % goal == 0) {
                return true;
            } else if (bigCountKilos < goal && bigCountKilos + smallCountKilos >= goal) {
                return true;
            } else {
                return false;
            }
        }
    }

    public static void main(String[] args) {
        System.out.println(canPack(4, 18, 19));
    }

The instance for 4,18,19 should return true but it doesn't. Could someone tell me what i'm doing wrong? Thanks in advance



Solution 1:[1]

int bigCountKilo = bigCount*5;
    if(bigCountKilo+smallCount*1 == goal){
        return true;
    }else if((bigCountKilo>=goal&&bigCountKilo%goal == 0)||(bigCountKilo>=goal&&goal%5<=smallCount)){
        return true;
    }else if(smallCount>=goal){
        return true;
    }else if(bigCountKilo+smallCount>goal&&goal%bigCountKilo<=smallCount){
        return true;
    }else{
        return false;
    }
}

This will work for all the conditions. Please try this and if anything wrong Kindly update.

Solution 2:[2]

Hey the explanation is pretty simple I hope you have understood the upper part until the bigCount, smallCount and goal.

So let's begin... let's assume goal = 9kg. It means you have to make a package that contains 9kg of flour. Now bigCount= 2 (means 10kg) and smallCount = 0.

How much flour you have (bigCount*5) + smallCount = 10kg do you have enough flour to make a package? Yes. Can you make a package of 9kg? No. Because you have two bags of 5kg each and you cannot take 1kg out from any bag.

Consider second scenario let's assume goal = 9kg. It means, you have to make a package that contains 9kg of flour.

Now bigCount= 1 (means 5kg) and smallCount = 5 (means 5kg).

Now you still have 10kg of flour but the question is, can you make a package without taking any flour from any bag? this time, Yes.

5kg + 1kg + 1kg + 1kg +1kg = 9kg and 1kg extra.

Simply telling you, you can have extra flour but you cannot take flour from bags just to reach the goal.

Solution 3:[3]

This code works on the greedy algorithm and passes all the test cases, give it a try:

public static boolean canPack(int bigCount, int smallCount, int goal) {
    // Here we are checking if the inputs are valid
    if (bigCount < 0 || smallCount < 0 || goal < 0 || (bigCount*5 + smallCount)  < goal) {
        return false;
    }

    // Here we multiply bigCount with 5 since each bigCount contains 5 kg
    int bigCountKilo = bigCount * 5;

    // We check if goal > 5 and then subtract bigCount from it till the goal is less than 5kg
    while (goal >= 5) {
        if (bigCountKilo > 0) {
            goal -= 5;
            bigCount-=1;
        } else {
            break;
        }
    }

    // Similarly here too we are checking if goal is greater than 0 and then subtracting smallCount values
    while (goal > 0) {
        if (smallCount > 0) {
            goal -= 1;
            smallCount -= 1;
        } else {
            break;
        }
    }

    // We are returning true if goal is reached (if goal becomes zero)
    return (goal == 0);
}

Solution 4:[4]

I feel like your logic behind finding the answer is not correct. The if statements don't cover all the cases and don't produce the right answer every time.

A basic approach would be something like:

  • Subtract 5 from goal, and subtract 1 from bigCount while goal > 0 and bigCount > 0
  • If goal is still > 0, check if smallCount >= goal. If yes, return true, else false.

Solution 5:[5]

private static boolean canPack(
    final int bigCount,
    final int smallCount,
    final int goal
) {

    // return goal - Math.min(goal / 5, bigCount) * 5 <= smallCount;

    final int numberOfBigNeeded = goal / 5;
    final int numberOfBigUsed = Math.min(numberOfBigNeeded, bigCount);
    final int remainingKilosFromGoalAfterUsingBig = goal - numberOfBigUsed * 5;
    final boolean hasEnoughSmallToCoverRemainingKilosFromGoalAfterUsingBig = remainingKilosFromGoalAfterUsingBig <= smallCount;
    return hasEnoughSmallToCoverRemainingKilosFromGoalAfterUsingBig;
}

Solution 6:[6]

public static boolean canPack(int bigCount, int smallCount, int goal) {
    // Input validation.
    if (bigCount < 0 || smallCount < 0 || goal < 0) {
        return false;
    }
    // Create a variable for the the bigCount variable to show it in kg.
    int bigCountInKg = bigCount * 5;
    
    if (bigCountInKg < goal) {
        int result = goal - bigCountInKg;
        if (result <= smallCount) {
            return true;
        } else {
            return false;
        }
    } else if (bigCountInKg == goal) {
        return true;
    } else if (bigCountInKg > goal) {
        int maxBigCount = goal / 5;
        int result = goal - maxBigCount;
        if (result <= smallCount) {
            return true;
        }
    }
    return false;
}

Solution 7:[7]

public static boolean canpack(int bigcount,int smallcount,int goal) {

    if(bigcount<0||smallcount<0||goal<0) {
        return false;
    }
    int a=bigcount*5;
    int b=smallcount*1;
    if(a+b>goal&&a<goal) {
        return true;
    }else if(a+b==goal) {
        return true;
    }else {
        return false;
    }
}

Hope it works.If not kindly inform me.

Solution 8:[8]

This is the solution for Flour packer issue

public class FlourPacker {

    public static boolean canPack(int bigCount, int smallCount, int goal) {
        boolean  bool=false;
        int bigCountKilos = bigCount * 5;
    
        if(bigCount == 0 && smallCount>=goal) {
             bool= true;
        } else if(smallCount==0 && bigCountKilos>= goal && goal%bigCountKilos==0) {
            bool= true;
        } else if (bigCount > 0 && smallCount>0 && goal >= 0) {
            int bigCountTotal= bigCount*5;
            int total=bigCountTotal+smallCount;
            if(total==goal){
                 bool= true;
            }
            
            if(goal/5<bigCount && goal%5 <=smallCount){
                bool= true;
            }
            
            if(bigCountTotal<=goal && goal-bigCountTotal <=smallCount) {
                bool= true;
            }
        }
        
        return bool;
    }
}

Solution 9:[9]

Here is my solution:

public static boolean canPack(int bigCount, int smallCount, int goal) {
    if ((bigCount < 0) || (smallCount < 0) || (goal < 0)) {
      return false;
    }

    for (int i = 0; i <= bigCount; i++){
      for (int j = 0; j <= smallCount; j++) {
        if (i*5 + j == goal){
          return true;
        }
      }
    }
    return false;
        
  }

I used nested loop to iterate all possible way to reach the goal. But I found another solution online which is much simpler so I would like to share with you guys too. credit to Vincent's corner.

public class FlourPacker {
    public static boolean canPack(int bigCount, int smallCount, int goal) {

        if (bigCount < 0 || smallCount < 0 || goal < 0) {
            return false;
        }

        if (bigCount * 5 > goal) {
            return smallCount >= goal % 5;
        }

        return smallCount >= goal - (bigCount * 5);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ANKUSH KUMAR
Solution 2 pasanjg
Solution 3 nik7
Solution 4 Sidak
Solution 5
Solution 6 Horváth Zoltán
Solution 7 Ayush Anand
Solution 8 hiddeneyes02
Solution 9 Ha Ngoc Yen