Minimize number of boxes by putting small box inside bigger one | C ++ implementation to minimize the number of the box by putting small box inside the bigger one ; Function to minimize the count ; Initial number of box ; Sort array of box size in increasing order ; check is current box size is smaller than next box size ; Decrement box count Increment current box count Increment next box count ; Check if both box have same size ; Print the result ; Driver code
#include <bits/stdc++.h> NEW_LINE 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 ; }