human
Maximum product of a triplet ( subsequence of size 3 ) in 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.
Sort the array in ascending order.
Return the maximum of product of last three elements and product of first two elements and last element.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to maximum product of a triplet ( subsequence of size 3 ) in array and the other steps required.
 def maxProduct ( arr , n ) :
	if n < 3 :
		return - 1
	arr . sort ( )
	return max ( arr [ 0 ] * arr [ 1 ] * arr [ n - 1 ] , arr [ n - 1 ] * arr [ n - 2 ] * arr [ n - 3 ] )
if __name__ == " _ _ main _ _ " :
	arr = [ - 10 , - 3 , 5 , 6 , - 20 ]
	n = len ( arr )
	_max = maxProduct ( arr , n )
	if _max == - 1 :
		print ( " No ▁ Triplet ▁ Exists " )
	else :
		print ( " Maximum ▁ product ▁ is " , _max )