Wednesday, 1 July 2015

FULFILLING AN ORDER WITH MINIMUM NUMBER OF BOTTLES - A JAVA SOLUTION

A bottled water industry based in USA, Michigan serves packaged water bottles of various sizes. The bottle size could be 1 litre, 5 litre, 7 litre and 10 litre. The owner wants to supply maximum quantity using fewer water bottles.

Your objective is to find the minimum number of bottles required to supply the given demand of water.

Example: if the demand of water is 25 litre, it can be supplied with minimum of 3 bottles:  2 BOTTLES OF SIZES 10 LITRE + 1 BOTTLE OF SIZE 5 LITRE

INPUT SPECIFICATION:

   Input will be the demand of water in litre (Integer)

OUTPUT SPECIFICATION:

  Output will be the Minimum number of bottles required to full-fill given demand of water (Integer)

Example:

Input : 12             (provide as an cmd line argument)
output : 2

Explanation: The demand of water is 12 litre that can be supplied using minimum of 2 bottles of sizes 7 litre and 5 litre.

Note: For better understanding,  please download the code - which is properly commented

Source Download: MinimumBottlesCode.java

SOLUTION:

public class MinimumBottlesCode {

 public static void main(String[] args) {

  // pass in quantity through command-line arguments

  int quantity = Integer.parseInt(args[0]);
  System.out.println(minimumBottles(quantity));
 }

 public static int minimumBottles(int quantity) {

  /*
   * Company sells water bottles of 1litres, 5 litres, 7litres, & 10
   * litres
   */

  int nums[] = { 7, 5, 10 };
  int minBottles = 0;
  int result = 0;
  /*
   * Iterate the given bottles of sizes - 5 litres, 7litres, and
   * 10litres(without 1litres). For Each Iteration : It finds the number
   * of bottles required to fullfill the given quantity for the order. The
   * final result is the least number of bottles in all the iterations.
   */
  for (int i = 0; i < nums.length; i++) {

   /*
    * say first dividing the given quantity 43 with 7 gives a
    * perfectDivs of 6 and remaining as 1, if the remaining is less
    * than 5 - this quantity is satisfied by 1litre bottles so - 6
    * bottles of 7 litre + 1 bottle of 1 litre
    */

   int perfectDivs = quantity / nums[i];

   int remaining = quantity % nums[i];

   if (remaining == 0) {
    minBottles = perfectDivs;

   } else if (remaining == 5) {
    minBottles = perfectDivs + 1;

   } else if (remaining == 7) {
    minBottles = perfectDivs + 1;

   } else if (remaining > 7 && remaining < 10) {
    minBottles = perfectDivs + remaining % 7 + 1;

   } else if (remaining > 5 && remaining < 7) {
    minBottles = perfectDivs + remaining % 5 + 1;

   } else if (remaining < 5) { // satisfied by 1 litre bottles
    minBottles = perfectDivs + remaining;

   }

   if (i == 0) {
    result = minBottles;
   }
   if (result > minBottles) {
    result = minBottles;
   }

  }

  return result;
 }
}

Please share : Hope it is a useful article :)