# Minimize number of boxes by putting small box inside bigger one

• Difficulty Level : Medium
• Last Updated : 30 Dec, 2022

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 a bigger one.
Note: Only one small box can fit inside one box.
Examples:

Input: size[] = {1, 2, 3}
Output:
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:
Explanation:
Put the box of size 1, 2, 3, 4 and 7 together and for the second box put 1 and 2 together. At last 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 the current box pointing variable, else move next pointing variable further.

Below is the implementation of the above approach:

## C++

 `// C++ implementation to minimize the``// number of the box by putting small``// box inside the bigger one` `#include ``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;``}`

## Java

 `// 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`

## Python3

 `# 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`

## C#

 `// 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`

## Javascript

 ``

Output

`1`

Time Complexity: O(N*logN) as Arrays.sort() method is used.
Auxiliary Space: O(1)

Approach: The key observation in the problem is that the minimum number of boxes is same as the maximum frequency of any element in the array. Because the rest of the elements are adjusted into one another.  Below is the illustration with the help of steps:

• Create a Hash-map to store the frequency of the elements.
• Finally, after maintaining the frequency return the maximum frequency of the element.

Below is the implementation of the above approach:

## C++

 `// C++ implementation of the``// above approach``#include``using` `namespace` `std;` `// Function to count the boxes``// at the end``int` `countBoxes(vector<``int``>A) {``    ` `    ``// Default value if``    ``// array is empty``    ``int` `count = -1;``    ``int` `n = A.size();``    ` `    ``// Map to store frequencies``    ``unordered_map<``int``,``int``>Map;``    ` `    ``// Loop to iterate over the``    ``// elements of the array``    ``for` `(``int` `i=0; i count) count = val;``        ` `    ``}``        ` `    ``return` `count;``}``    ` `// Driver Code``int` `main(){``    ``vector<``int``>a = {8, 15, 1, 10, 5, 1};` `    ``// Function Call``    ``int` `minBoxes = countBoxes(a);``    ``cout<

## Java

 `// Java implementation of the``// above approach` `import` `java.util.*;` `public` `class` `Boxes {``  ` `    ``// Function to count the boxes``    ``// at the end``    ``int` `countBoxes(``int``[] A) {``      ` `        ``// Default value if``        ``// array is empty``        ``int` `count = -``1``;``        ``int` `n = A.length;``      ` `         ``// Map to store frequencies``        ``Map map =``          ``new` `HashMap<>();``      ` `        ``// Loop to iterate over the``        ``// elements of the array``        ``for` `(``int` `i=``0``; i count) count = val;``        ``}``        ``return` `count;``    ``}``    ` `    ``// Driver Code``    ``public` `static` `void` `main(``                  ``String[] args)``    ``{``        ``int``[] a = {``8``, ``15``, ``1``, ``10``, ``5``, ``1``};``        ``Boxes obj = ``new` `Boxes();``      ` `        ``// Function Call``        ``int` `minBoxes = obj.countBoxes(a);``          ``System.out.println(minBoxes);``    ``}``}`

## Python3

 `# Python implementation of the``# above approach` `# Function to count the boxes``# at the end``def` `countBoxes(A):``    ` `    ``# Default value if``    ``# array is empty``   ``count ``=` `-``1``   ``n ``=` `len``(A)``    ` `   ``# Map to store frequencies``   ``map` `=` `{}``    ` `   ``# Loop to iterate over the``   ``# elements of the array``   ``for` `i ``in` `range``(n):``      ``key ``=` `A[i]``            ` `      ``if``(key ``in` `map``):``         ``map``[key] ``=` `map``[key]``+``1``      ``else``:``         ``map``[key] ``=` `1` `      ``val ``=` `map``[key]``        ` `      ``# Condition to get the maximum``      ``# value of the key``      ``if` `(val > count):``         ``count ``=` `val``        ` `   ``return` `count``    ` `# Driver Code``a ``=` `[``8``, ``15``, ``1``, ``10``, ``5``, ``1``]` `# Function Call``minBoxes ``=` `countBoxes(a)``print``(minBoxes)` `# This code is contributed by shinjanpatra.`

## C#

 `// Include namespace system``using` `System;``using` `System.Collections.Generic;` `using` `System.Collections;` `public` `class` `Boxes``{` `  ``// Function to count the boxes``  ``// at the end``  ``public` `int` `countBoxes(``int``[] A)``  ``{` `    ``// Default value if``    ``// array is empty``    ``var` `count = -1;``    ``var` `n = A.Length;` `    ``// Map to store frequencies``    ``var` `map = ``new` `Dictionary<``int``, ``int``>();` `    ``// Loop to iterate over the``    ``// elements of the array``    ``for` `(``int` `i = 0; i < n; i++)``    ``{``      ``var` `key = A[i];``      ``map[key] = (map.ContainsKey(key) ? map[key] : 0) + 1;``      ``var` `val = map[key];` `      ``// Condition to get the maximum``      ``// value of the key``      ``if` `(val > count)``      ``{``        ``count = val;``      ``}``    ``}``    ``return` `count;``  ``}` `  ``// Driver Code``  ``public` `static` `void` `Main(String[] args)``  ``{``    ``int``[] a = {8, 15, 1, 10, 5, 1};``    ``var` `obj = ``new` `Boxes();` `    ``// Function Call``    ``var` `minBoxes = obj.countBoxes(a);``    ``Console.WriteLine(minBoxes);``  ``}``}` `// This code is contributed by aadityaburujwale.`

## Javascript

 ``

Output

`2`

Time Complexity: O(N)
Auxiliary Space: O(N)

My Personal Notes arrow_drop_up
{"found_math": false, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "url": "https://www.geeksforgeeks.org/minimize-number-of-boxes-by-putting-small-box-inside-bigger-one/?ref=lbp", "fetch_time": 1679797410000000000, "content_mime_type": "text/html", "warc_filename": "crawl-data/CC-MAIN-2023-14/segments/1679296945381.91/warc/CC-MAIN-20230326013652-20230326043652-00646.warc.gz", "warc_record_offset": 860173980, "warc_record_length": 33599, "token_count": 3597, "char_count": 10886, "score": 3.6875, "int_score": 4, "crawl": "CC-MAIN-2023-14", "snapshot_type": "latest", "language": "en", "language_score": 0.7096409201622009, "file_path": "/sailhome/yjruan/project/latent-lm/data/processed/finemath-4plus/finemath-4plus/train-00048-of-00064.parquet"}