- Binary Search, Divide and Conquer, Mathematical, Searching

Maximize boxes required to keep at least one black and one white shirt

Maximize boxes required to keep at least one black and one white shirt Given three numbers W, B, and O representing the quantities of white, black, and other colors shirts respectively, the task is to find the maximum number of boxes required such that each box contains three shirts consisting of at least one white and black shirt using the given quantity of shirts.Examples:Input: W = 3, B = 3, O = 1Output: 2Explanation:Below are the distribution of shirts in the boxes:Box 1: 1 white shirt, 1 black shirt and 1 shirt of other color.Box 2: 2 white shirts and 1 black shirt.Box 3: 1 black shirtSince, only 2 boxes satisfy the given condition which is the maximum possible number of boxes with the given quantity of shirts. Therefore, print 2.Input: W = 4, B = 6, O = 0Output: 3Approach: The given problem can be solved by using the Binary Search. The idea is to search for the maximum number of boxes in the search space formed by the lower and upper bound. It can be observed that the lower bound and the upper bound of the count of the box is 0 and minimum of W and B respectively. Follow the steps below to solve the problem:Initialize a variable, say ans as 0 to store the required result.Initialize two variables, say low as 0 and high as the minimum of (W, B).Loop until the value of low is less than high and perform the following steps:Find the middle value of low and high in a variable, say mid.Check if the maximum number of boxes can be equal to mid, then update the value of ans to mid and update the search space in the right half by updating the value of low.Otherwise, update the search space to the left half by updating the value of high.Print the value of ans as the result.Below is the implementation of the above approach:C++  #include using namespace std;  void numberofBoxes(int W, int B, int O){            int low = 0, high = min(W, B);          int ans = 0;          while (low = mid) and (B >= mid))            and ((W – mid) + (B – mid) + O)                    >= mid) {                                      ans = mid;            low = mid + 1;        }                  else            high = mid – 1;    }          cout