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: 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. 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++
#include <bits/stdc++.h>
using namespace std;
void minBox( int arr[], int n)
{
int box = n;
sort(arr, arr + n);
int curr_box = 0, next_box = 1;
while (curr_box < n && next_box < n) {
if (arr[curr_box] < arr[next_box]) {
box--;
curr_box++;
next_box++;
}
else if (arr[curr_box] == arr[next_box])
next_box++;
}
cout << box << endl;
}
int main()
{
int size[] = { 1, 2, 3 };
int n = sizeof (size) / sizeof (size[0]);
minBox(size, n);
return 0;
}
|
Java
import java.util.Arrays;
class GFG{
public static void minBox( int arr[], int n)
{
int box = n;
Arrays.sort(arr);
int curr_box = 0 , next_box = 1 ;
while (curr_box < n && next_box < n)
{
if (arr[curr_box] < arr[next_box])
{
box--;
curr_box++;
next_box++;
}
else if (arr[curr_box] ==
arr[next_box])
next_box++;
}
System.out.println(box);
}
public static void main(String args[])
{
int []size = { 1 , 2 , 3 };
int n = size.length;
minBox(size, n);
}
}
|
Python3
def minBox(arr, n):
box = n
arr.sort()
curr_box, next_box = 0 , 1
while (curr_box < n and next_box < n):
if (arr[curr_box] < arr[next_box]):
box = box - 1
curr_box = curr_box + 1
next_box = next_box + 1
elif (arr[curr_box] = = arr[next_box]):
next_box = next_box + 1
print (box)
size = [ 1 , 2 , 3 ]
n = len (size)
minBox(size, n)
|
C#
using System;
class GFG{
public static void minBox( int []arr, int n)
{
int box = n;
Array.Sort(arr);
int curr_box = 0, next_box = 1;
while (curr_box < n && next_box < n)
{
if (arr[curr_box] < arr[next_box])
{
box--;
curr_box++;
next_box++;
}
else if (arr[curr_box] ==
arr[next_box])
next_box++;
}
Console.WriteLine(box);
}
public static void Main(String []args)
{
int []size = { 1, 2, 3 };
int n = size.Length;
minBox(size, n);
}
}
|
Javascript
<script>
function minBox(arr, n)
{
var box = n;
arr.sort();
var curr_box = 0, next_box = 1;
while (curr_box < n && next_box < n)
{
if (arr[curr_box] < arr[next_box])
{
box--;
curr_box++;
next_box++;
}
else if (arr[curr_box] ==
arr[next_box])
next_box++;
}
document.write(box);
}
var size = [ 1, 2, 3 ];
var n = size.length;
minBox(size, n);
</script>
|
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++
#include<bits/stdc++.h>
using namespace std;
int countBoxes(vector< int >A) {
int count = -1;
int n = A.size();
unordered_map< int , int >Map;
for ( int i=0; i<n; i++) {
int key = A[i];
Map[key]++;
int val = Map[key];
if (val > count) count = val;
}
return count;
}
int main(){
vector< int >a = {8, 15, 1, 10, 5, 1};
int minBoxes = countBoxes(a);
cout<<minBoxes<<endl;
}
|
Java
import java.util.*;
public class Boxes {
int countBoxes( int [] A) {
int count = - 1 ;
int n = A.length;
Map<Integer, Integer> map =
new HashMap<>();
for ( int i= 0 ; i<n; i++) {
int key = A[i];
map.put(key,
map.getOrDefault(key, 0 ) + 1 );
int val = map.get(key);
if (val > count) count = val;
}
return count;
}
public static void main(
String[] args)
{
int [] a = { 8 , 15 , 1 , 10 , 5 , 1 };
Boxes obj = new Boxes();
int minBoxes = obj.countBoxes(a);
System.out.println(minBoxes);
}
}
|
Python3
def countBoxes(A):
count = - 1
n = len (A)
map = {}
for i in range (n):
key = A[i]
if (key in map ):
map [key] = map [key] + 1
else :
map [key] = 1
val = map [key]
if (val > count):
count = val
return count
a = [ 8 , 15 , 1 , 10 , 5 , 1 ]
minBoxes = countBoxes(a)
print (minBoxes)
|
C#
using System;
using System.Collections.Generic;
using System.Collections;
public class Boxes
{
public int countBoxes( int [] A)
{
var count = -1;
var n = A.Length;
var map = new Dictionary< int , int >();
for ( int i = 0; i < n; i++)
{
var key = A[i];
map[key] = (map.ContainsKey(key) ? map[key] : 0) + 1;
var val = map[key];
if (val > count)
{
count = val;
}
}
return count;
}
public static void Main(String[] args)
{
int [] a = {8, 15, 1, 10, 5, 1};
var obj = new Boxes();
var minBoxes = obj.countBoxes(a);
Console.WriteLine(minBoxes);
}
}
|
Javascript
<script>
function countBoxes(A) {
let count = -1;
let n = A.length;
let map = new Map();
for (let i=0; i<n; i++) {
let key = A[i];
if (map.has(key)){
map.set(key,map.get(key)+1);
}
else map.set(key,1);
let val = map.get(key);
if (val > count) count = val;
}
return count;
}
let a = [8, 15, 1, 10, 5, 1];
let minBoxes = countBoxes(a);
document.write(minBoxes, "</br>" );
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N)