Maximize boxes required to keep at least one black and one white shirt
Last Updated :
03 Sep, 2021
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 = 1
Output: 2
Explanation:
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 shirt
Since, 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 = 0
Output: 3
Approach: 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 <bits/stdc++.h>
using namespace std;
void numberofBoxes( int W, int B, int O)
{
int low = 0, high = min(W, B);
int ans = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (((W >= mid) and (B >= mid))
and ((W - mid) + (B - mid) + O)
>= mid) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
cout << ans;
}
int main()
{
int W = 3, B = 3, O = 1;
numberofBoxes(W, B, O);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void numberofBoxes( int W, int B, int O)
{
int low = 0 , high = Math.min(W, B);
int ans = 0 ;
while (low <= high)
{
int mid = low + (high - low) / 2 ;
if (((W >= mid) && (B >= mid)) &&
((W - mid) + (B - mid) + O) >= mid)
{
ans = mid;
low = mid + 1 ;
}
else
high = mid - 1 ;
}
System.out.println(ans);
}
public static void main(String args[])
{
int W = 3 , B = 3 , O = 1 ;
numberofBoxes(W, B, O);
}
}
|
Python3
def numberofBoxes(W, B, O):
low = 0
high = min (W, B)
ans = 0
while (low < = high):
mid = low + (high - low) / / 2
if (((W > = mid) and (B > = mid)) and ((W - mid) + (B - mid) + O) > = mid):
ans = mid
low = mid + 1
else :
high = mid - 1
print (ans)
if __name__ = = '__main__' :
W = 3
B = 3
O = 1
numberofBoxes(W, B, O)
|
C#
using System;
class GFG {
static void numberofBoxes( int W, int B, int O)
{
int low = 0, high = Math.Min(W, B);
int ans = 0;
while (low <= high) {
int mid = low + (high - low) / 2;
if (((W >= mid) &&(B >= mid))
&&((W - mid) + (B - mid) + O)
>= mid) {
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
Console.Write(ans);
}
public static void Main()
{
int W = 3, B = 3, O = 1;
numberofBoxes(W, B, O);
}
}
|
Javascript
<script>
function numberofBoxes(W, B, O)
{
let low = 0, high = Math.min(W, B);
let ans = 0;
while (low <= high)
{
let mid = low + Math.floor((high - low) / 2);
if (((W >= mid) && (B >= mid)) &&
((W - mid) + (B - mid) + O) >= mid)
{
ans = mid;
low = mid + 1;
}
else
high = mid - 1;
}
document.write(ans);
}
let W = 3, B = 3, O = 1;
numberofBoxes(W, B, O);
</script>
|
Time Complexity: O(log(min(W, B)))
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...