Suppose we have an array called nums and this contains positive and negative numbers. We have another value k. We have to check whether any subarray whose product is k is present in the array or not.
So, if the input is like nums = [-2,-1,1,3,5,8], k = 6, then the output will be True as the subarray is [-2,-1,3]
To solve this, we will follow these steps −
Let us see the following implementation to get better understanding −
def solve(nums, k): minimum = nums maximum = nums prod_max = nums for i in range( 1, len(nums)): if nums[i] < 0: maximum, minimum = minimum, maximum maximum = max(nums[i], maximum * nums[i]) minimum = min(nums[i], minimum * nums[i]) if minimum == k or maximum == k: return True prod_max = max(prod_max, maximum) return False nums = [-2,-1,1,3,5,8] k = 6 print(solve(nums, k))