Maximum value of expression ( arr [ i ] + arr [ j ] * arr [ k ] ) formed from a valid Triplet | Java program for the above approach ; 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
import java . util . Scanner ; class GFG { static void max_valid_triplet ( int A [ ] , int n ) { int ans = - 1 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( A [ i ] < A [ j ] && A [ j ] < A [ k ] ) { int value = A [ i ] + A [ j ] * A [ k ] ; if ( value > ans ) { ans = value ; } } } } } System . out . println ( ans ) ; } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 7 , 9 , 3 , 8 , 11 , 10 } ; int n = arr . length ; max_valid_triplet ( arr , n ) ; } }