human
Maximum product of subsequence of size k | Required function.
sorting given input array.
variable to store final product of all element of sub - sequence of size k.
CASE I If max element is 0 and k is odd then max product will be 0.
CASE II If all elements are negative and k is odd then max product will be product of rightmost - subarray of size k.
else i is current left pointer index.
j is current right pointer index.
CASE III if k is odd and rightmost element in sorted array is positive then it must come in subsequence Multiplying A [ j ] with product and correspondingly changing j.
CASE IV Now k is even . So , Now we deal with pairs Each time a pair is multiplied to product ie . . two elements are added to subsequence each time . Effectively k becomes half Hence , k >>= 1 means k /= 2.
Now finding k corresponding pairs to get maximum possible value of product.
product from left pointers.
product from right pointers.
Taking the max product from two choices . Correspondingly changing the pointer 's position.
Finally return product.
Driver Code"
Solution in Python:
 ... What is a code to maximum product of subsequence of size k and the other steps required?
assistant
#Here is code to maximum product of subsequence of size k and the other steps required.
 def maxProductSubarrayOfSizeK ( A , n , k ) :
	A . sort ( )
	product = 1
	if ( A [ n - 1 ] == 0 and ( k & 1 ) ) :
		return 0
	if ( A [ n - 1 ] <= 0 and ( k & 1 ) ) :
		for i in range ( n - 1 , n - k + 1 , - 1 ) :
			product *= A [ i ]
		return product
	i = 0
	j = n - 1
	if ( k & 1 ) :
		product *= A [ j ]
		j -= 1
		k -= 1
	k >>= 1
	for itr in range ( k ) :
		left_product = A [ i ] * A [ i + 1 ]
		right_product = A [ j ] * A [ j - 1 ]
		if ( left_product > right_product ) :
			product *= left_product
			i += 2
		else :
			product *= right_product
			j -= 2
	return product
if __name__ == " _ _ main _ _ " :
	A = [ 1 , 2 , - 1 , - 3 , - 6 , 4 ]
	n = len ( A )
	k = 4
	print ( maxProductSubarrayOfSizeK ( A , n , k ) )