[14 Feb 2020] Maximum Product Subarray
Maximum Product Subarray
Given an array that contains both positive and negative integers, find the product of the maximum product subarray. Expected Time complexity is O(n) and only O(1) extra space can be used.
Examples:
Input: arr[] = {6, -3, -10, 0, 2}
Output: 180 // The subarray is {6, -3, -10}
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60 // The subarray is {60}
Input: arr[] = {-2, -3, 0, -2, -40}
Output: 80 // The subarray is {-2, -40}
It is similar to Largest Sum Contiguous Subarray problem. The only thing to note here is, maximum product can also be obtained by minimum (negative) product ending with the previous element multiplied by this element. For example, in array {12, 2, -3, -5, -6, -2}, when we are at element -2, the maximum product is multiplication of, minimum product ending with -6 and -2.
Output:
Maximum Sub array product is 112
Time Complexity: O(n)
Auxiliary Space: O(1)
Auxiliary Space: O(1)
Output:
Maximum Sub array product is 112
Time Complexity: O(n)
Auxiliary Space: O(1)
Auxiliary Space: O(1)
Output:
Maximum Sub array product is 112
Time Complexity: O(n)
Auxiliary Space: O(1)
Auxiliary Space: O(1)
Comments
Post a Comment