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 ; Given array arr [ ] ; Function call
function max_valid_triplet ( A , n ) { let ans = - 1 ; for ( let i = 0 ; i < n - 2 ; i ++ ) { for ( let j = i + 1 ; j < n - 1 ; j ++ ) { for ( let k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { let value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } document . write ( ans ) ; } let arr = [ 7 , 9 , 3 , 8 , 11 , 10 ] ; let n = arr . length ; max_valid_triplet ( arr , n ) ;