Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | Function that generate all valid triplets and calculate the value of the valid triplets ; Generate all triplets ; Check whether the triplet is valid or not ; Update the value ; Print the maximum value ; Driver Code ; Given array arr ; Function call
def max_valid_triplet ( A , n ) : NEW_LINE INDENT ans = - 1 ; NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( A [ i ] < A [ j ] and A [ j ] < A [ k ] ) : NEW_LINE INDENT value = A [ i ] + A [ j ] * A [ k ] ; NEW_LINE if ( value > ans ) : NEW_LINE INDENT ans = value ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 7 , 9 , 3 , 8 , 11 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE max_valid_triplet ( arr , n ) ; NEW_LINE DEDENT