Maximize number of groups formed with size not smaller than its largest element | Function that prints 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 ; Printing maximum number of groups ; Driver Code
def makeGroups ( a , n ) : NEW_LINE INDENT v = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT v [ a [ i ] ] += 1 NEW_LINE DEDENT no_of_groups = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT no_of_groups += v [ i ] // i NEW_LINE v [ i ] = v [ i ] % i NEW_LINE DEDENT i = 1 NEW_LINE total = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( v [ i ] != 0 ) : NEW_LINE INDENT total = v [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE while ( i <= n ) : NEW_LINE INDENT if ( v [ i ] != 0 ) : NEW_LINE INDENT total += v [ i ] NEW_LINE if ( total >= i ) : NEW_LINE INDENT rem = total - i NEW_LINE no_of_groups += 1 NEW_LINE total = rem NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT print ( no_of_groups ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 2 , 2 ] NEW_LINE size = len ( arr ) NEW_LINE makeGroups ( arr , size ) NEW_LINE DEDENT