Maximize number of groups formed with size not smaller than its largest element | Function that prlets the number of maximum groups ; Store the number of occurrence of elements ; Make all groups of similar elements and store the left numbers ; Condition for finding first leftover element ; Condition for current leftover element ; Condition if group size is equal to or more than current element ; Prleting maximum number of groups ; Driver Code
function makeGroups ( a , n ) { let v = Array . from ( { length : n + 1 } , ( _ , i ) => 0 ) ; for ( let i = 0 ; i < n ; i ++ ) { v [ a [ i ] ] ++ ; } let no_of_groups = 0 ; for ( let i = 1 ; i <= n ; i ++ ) { no_of_groups += Math . floor ( v [ i ] / i ) ; v [ i ] = v [ i ] % i ; } let i = 1 ; let total = 0 ; for ( i = 1 ; i <= n ; i ++ ) { if ( v [ i ] != 0 ) { total = v [ i ] ; break ; } } i ++ ; while ( i <= n ) { if ( v [ i ] != 0 ) { total += v [ i ] ; if ( total >= i ) { let rem = total - i ; no_of_groups ++ ; total = rem ; } } i ++ ; } document . write ( no_of_groups + " " ) ; } let arr = [ 2 , 3 , 1 , 2 , 2 ] ; let size = arr . length ; makeGroups ( arr , size ) ;