Form smallest number using indices of numbers chosen from Array with sum less than S | Function to find the minimum number which have maximum length ; Find Maximum length of number ; Find minimum number WHich have maximum length ; Driver Code
def max_number ( arr , sum ) : NEW_LINE INDENT frac = [ 0 ] * 9 NEW_LINE maxi = - 10 ** 9 NEW_LINE pos = 0 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT frac [ i ] = sum // arr [ i ] NEW_LINE if ( frac [ i ] > maxi ) : NEW_LINE INDENT pos = i NEW_LINE maxi = frac [ i ] NEW_LINE DEDENT DEDENT an = str ( ( pos + 1 ) ) * maxi NEW_LINE sum -= maxi * arr [ pos ] NEW_LINE ans = [ i for i in an ] NEW_LINE for i in range ( maxi ) : NEW_LINE INDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT if ( sum + arr [ pos ] - arr [ j - 1 ] >= 0 ) : NEW_LINE INDENT ans [ i ] = str ( j ) NEW_LINE sum += arr [ pos ] - arr [ j - 1 ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( maxi == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return " " . join ( ans ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 2 , 4 , 6 , 5 , 4 , 2 , 3 ] NEW_LINE s = 13 NEW_LINE print ( max_number ( arr , s ) ) NEW_LINE DEDENT