Minimize number of boxes by putting small box inside bigger one

Given an array size[] of box sizes, our task is to find the number of boxes left at the end, after putting the smaller sized box into bigger one.

Note: Only one small box can fit inside one box.

Examples:

Input: size[] = {1, 2, 3}
Output: 1
Explanation:
Here, box of size 1 can fit inside box of size 2 and the box of size 2 can fit inside box of size 3. So at last we have only one box of size 3.

Input: size[] = {1, 2, 2, 3, 7, 4, 2, 1}
Output: 3
Explanation:
Put the box of size 1, 2, 3, 4 and 7 together and for the second box put 1 and 2 together. Atlast 2 is left which will not fit inside anyone. So we have 3 boxes left.



Approach: The idea is to follow the steps given below:

  • Sort the given array size[] in increasing order and check if the current box size is greater than the next box size. If yes then decrease the initial box number.
  • Otherwise, if the current box size is equal to next box size, then check if the current box can fit inside next to next box size. If yes then move current box pointing variable, else move next pointing variable further.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation to minimize the
// number of the box by putting small
// box inside the bigger one
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to minimize the count
void minBox(int arr[], int n)
{
    // Initial number of box
    int box = n;
  
    // Sort array of box size
    // in increasing order
    sort(arr, arr + n);
  
    int curr_box = 0, next_box = 1;
    while (curr_box < n && next_box < n) {
  
        // check is current box size
        // is smaller than next box size
        if (arr[curr_box] < arr[next_box]) {
  
            // Decrement box count
            // Increment current box count
            // Increment next box count
            box--;
            curr_box++;
            next_box++;
        }
  
        // Check if both box
        // have same size
        else if (arr[curr_box] == arr[next_box])
            next_box++;
    }
  
    // Print the result
    cout << box << endl;
}
  
// Driver code
int main()
{
    int size[] = { 1, 2, 3 };
    int n = sizeof(size) / sizeof(size[0]);
    minBox(size, n);
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation to minimize the
// number of the box by putting small
// box inside the bigger one
import java.util.Arrays; 
  
class GFG{
      
// Function to minimize the count
public static void minBox(int arr[], int n)
{
      
    // Initial number of box
    int box = n;
  
    // Sort array of box size
    // in increasing order
    Arrays.sort(arr);
  
    int curr_box = 0, next_box = 1;
    while (curr_box < n && next_box < n) 
    {
          
        // Check is current box size
        // is smaller than next box size
        if (arr[curr_box] < arr[next_box])
        {
              
            // Decrement box count
            // Increment current box count
            // Increment next box count
            box--;
            curr_box++;
            next_box++;
        }
  
        // Check if both box
        // have same size
        else if (arr[curr_box] == 
                 arr[next_box])
            next_box++;
    }
  
    // Print the result
    System.out.println(box);
}
  
// Driver code
public static void main(String args[])
{
    int []size = { 1, 2, 3 };
    int n = size.length;
      
    minBox(size, n);
}
}
  
// This code is contributed by SoumikMondal

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation to minimize the 
# number of the box by putting small 
# box inside the bigger one
  
# Function to minimize the count 
def minBox(arr, n): 
      
    # Initial number of box 
    box = n
  
    # Sort array of box size 
    # in increasing order 
    arr.sort()
  
    curr_box, next_box = 0, 1
    while (curr_box < n and next_box < n): 
  
        # Check is current box size 
        # is smaller than next box size 
        if (arr[curr_box] < arr[next_box]): 
  
            # Decrement box count 
            # Increment current box count 
            # Increment next box count 
            box = box - 1
            curr_box = curr_box + 1
            next_box = next_box + 1
  
        # Check if both box 
        # have same size 
        elif (arr[curr_box] == arr[next_box]): 
            next_box = next_box + 1
  
    # Print the result 
    print(box) 
  
# Driver code
size = [ 1, 2, 3
n = len(size) 
  
minBox(size, n) 
  
# This code is contributed by divyeshrabadiya07

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation to minimize the
// number of the box by putting small
// box inside the bigger one
using System;
  
class GFG{
      
// Function to minimize the count
public static void minBox(int []arr, int n)
{
      
    // Initial number of box
    int box = n;
  
    // Sort array of box size
    // in increasing order
    Array.Sort(arr);
  
    int curr_box = 0, next_box = 1;
    while (curr_box < n && next_box < n) 
    {
          
        // Check is current box size
        // is smaller than next box size
        if (arr[curr_box] < arr[next_box])
        {
              
            // Decrement box count
            // Increment current box count
            // Increment next box count
            box--;
            curr_box++;
            next_box++;
        }
  
        // Check if both box
        // have same size
        else if (arr[curr_box] ==
                 arr[next_box])
            next_box++;
    }
  
    // Print the result
    Console.WriteLine(box);
}
  
// Driver code
public static void Main(String []args)
{
    int []size = { 1, 2, 3 };
    int n = size.Length;
      
    minBox(size, n);
}
}
  
// This code is contributed by Amit Katiyar

chevron_right


Output:

1

Time Complexity: O(N)

Auxiliary Space: O(1)

competitive-programming-img




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.