Maximum product of a triplet ( subsequence of size 3 ) in array | A O ( n ) C # 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
using System ; using System . Collections ; class GFG { static int maxProduct ( int [ ] arr , int n ) { if ( n < 3 ) return - 1 ; int maxA = Int32 . MinValue , maxB = Int32 . MinValue , maxC = Int32 . MinValue ; int minA = Int32 . MaxValue , minB = Int32 . MaxValue ; 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 ) Console . Write ( " No ▁ Triplet ▁ Exists " ) ; else Console . Write ( " Maximum ▁ product ▁ is ▁ " + max ) ; } }