Maximum product quadruple ( sub | Function to find a maximum product of a quadruple in array of integers of size n ; if size is less than 4 , no quadruple exists ; Initialize Maximum , second maximum , third maximum and fourth maximum element ; Initialize Minimum , second minimum , third minimum and fourth minimum element ; Update Maximum , second maximum , third maximum and fourth maximum element ; Update second maximum , third maximum and fourth maximum element ; Update third maximum and fourth maximum element ; Update fourth maximum element ; Update Minimum , second minimum , third minimum and fourth minimum element ; Update second minimum , third minimum and fourth minimum element ; Update third minimum and fourth minimum element ; Update fourth minimum element ; Return the maximum of x , y and z ; Driver Code
< ? php function maxProduct ( $ arr , $ n ) { if ( $ n < 4 ) return -1 ; $ maxA = -2147483648 ; $ maxB = -2147483648 ; $ maxC = -2147483648 ; $ maxD = -2147483648 ; $ minA = 2147483647 ; $ minB = 2147483647 ; $ minC = 2147483647 ; $ minD = 2147483647 ; for ( $ i = 0 ; $ i < $ n ; $ i ++ ) { if ( $ arr [ $ i ] > $ maxA ) { $ maxD = $ maxC ; $ maxC = $ maxB ; $ maxB = $ maxA ; $ maxA = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] > $ maxB ) { $ maxD = $ maxC ; $ maxC = $ maxB ; $ maxB = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] > $ maxC ) { $ maxD = $ maxC ; $ maxC = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] > $ maxD ) $ maxD = $ arr [ $ i ] ; if ( $ arr [ $ i ] < $ minA ) { $ minD = $ minC ; $ minC = $ minB ; $ minB = $ minA ; $ minA = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] < $ minB ) { $ minD = $ minC ; $ minC = $ minB ; $ minB = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] < $ minC ) { $ minD = $ minC ; $ minC = $ arr [ $ i ] ; } elseif ( $ arr [ $ i ] < $ minD ) $ minD = $ arr [ $ i ] ; } $ x = $ maxA * $ maxB * $ maxC * $ maxD ; $ y = $ minA * $ minB * $ minC * $ minD ; $ z = $ minA * $ minB * $ maxA * $ maxB ; return max ( $ x , max ( $ y , $ z ) ) ; } $ arr = array ( 1 , -4 , 3 , -6 , 7 , 0 ) ; $ n = count ( $ arr ) ; $ max = maxProduct ( $ arr , $ n ) ; if ( $ max == -1 ) echo " No ▁ Quadruple ▁ Exists " ; else echo " Maximum ▁ product ▁ is ▁ " . $ max ; ? >