Largest number with the given set of N digits that is divisible by 2 , 3 and 5 | Function to find the largest integer with the given set ; find sum of all the digits look if any 0 is present or not ; if 0 is not present , the resultant number won 't be divisible by 5 ; sort all the elements in a non - decreasing manner ; if there is just one element 0 ; find the remainder of the sum of digits when divided by 3 ; there can a remainder as 1 or 2 ; traverse from the end of the digits ; first element which has the same remainder , remove it ; if there is no element which has a same remainder as y ; subtract it by 3 ( could be one or two ) ; delete two minimal digits which has a remainder as y ; print all the digits as a single integer ; Driver code ; initialize the number of set of digits ; initialize all the set of digits in a vector
def findLargest ( n , v ) : NEW_LINE INDENT flag = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( v [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT sum += v [ i ] NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT else : NEW_LINE INDENT v . sort ( reverse = True ) NEW_LINE if ( v [ 0 ] == 0 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE return 0 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE y = sum % 3 NEW_LINE if ( y != 0 ) : NEW_LINE INDENT i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( flag == 0 ) : NEW_LINE INDENT y = 3 - y NEW_LINE cnt = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( v [ i ] % 3 == y ) : NEW_LINE INDENT v . remove ( v [ i ] ) NEW_LINE cnt += 1 NEW_LINE if ( cnt >= 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE DEDENT DEDENT DEDENT for i in ( v ) : NEW_LINE print ( i , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 11 NEW_LINE v = [ 3 , 9 , 9 , 6 , 4 , 3 , 6 , 4 , 9 , 6 , 0 ] NEW_LINE findLargest ( n , v ) NEW_LINE DEDENT