Minimize number of boxes by putting small box inside bigger one | Java 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
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 ) ; } }