Maximum product of a triplet ( subsequence of size 3 ) in array | A O ( n ) Java program to find maximum product pair in an array . ; Function to find a maximum product of a triplet in array of integers of size n ; If size is less than 3 , no triplet exists ; Initialize Maximum , second maximum and third maximum element ; Initialize Minimum and second minimum element ; Update Maximum , second maximum and third maximum element ; Update second maximum and third maximum element ; Update third maximum element ; Update Minimum and second minimum element ; Update second minimum element ; Driver code
import java . util . * ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int maxA = Integer . MIN_VALUE , maxB = Integer . MIN_VALUE , maxC = Integer . MIN_VALUE ; int minA = Integer . MAX_VALUE , minB = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > maxA ) { maxC = maxB ; maxB = maxA ; maxA = arr [ i ] ; } else if ( arr [ i ] > maxB ) { maxC = maxB ; maxB = arr [ i ] ; } else if ( arr [ i ] > maxC ) maxC = arr [ i ] ; if ( arr [ i ] < minA ) { minB = minA ; minA = arr [ i ] ; } else if ( arr [ i ] < minB ) minB = arr [ i ] ; } return Math . max ( minA * minB * maxA , maxA * maxB * maxC ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , - 4 , 3 , - 6 , 7 , 0 } ; int n = arr . length ; int max = maxProduct ( arr , n ) ; if ( max == - 1 ) System . out . print ( " No ▁ Triplet ▁ Exists " ) ; else System . out . print ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }