Javatpoint Logo
Javatpoint Logo

Maximum Product Subarray

Find the largest product of any subarray of an array containing positive and negative integers.

Examples

Input: array = [6, -4, -10, 1, 2]

Output: 240 (Its subarray is [6, -4, -10])

Input: array = [-2, -3, -11, 0, 61]

Output: 61 (Its subarray is [61])

Approach - 1

Follow the instructions below to write code for this problem:

The goal is to iterate over each consecutive subarray, determine the product of the elements of a particular subarray, and then return the greatest probable product from these products.

Below is the algorithmic approach to this method:

  • We will run a nested Python for loop to form every possible subarray of the given array
  • Then we will find the product of the elements of the present subarray
  • The function will return the maximum product value of all products calculated in the subarrays.

Here is the Python code for the algorithm mentioned above:

Code

Output:

The maximum product of one of the subarrays is: 24156

Time Complexity: The time complexity of this approach is O(N2), where N is the number of elements in the given array.

Auxiliary Space: Since we have just created the product and the prod variable to store the product values, the program will take extra space. Hence space complexity of this problem is O(1).

Approach - 2

Below is the idea of the second approach:

The assumption made by the following method is that the given array's output will always be positive. For each of the situations mentioned above, the answer applies. It is ineffective for arrays with values of 0, 0, 0, -10, 0, 0, 0, 0, etc. This approach is readily adaptable to address negative arrays. It is analogous to the problem at hand.

The lowest (negative) result ending with the preceding number multiplied by this number might also result in the maximum product. For instance, when we reach element 3 in this sample array "12, 3, -3, -5, -7, -3," the largest product results from the multiplication of the lowest product that ends with -7 and -3.

The largest product using the procedure mentioned above is equal to one if all integers of the input array are negative. So, if the largest product is 1, we must return the largest array entry.

Below is the algorithmic approach to the idea above:

  • We will declare two variables, max_product, and min_product. The max_product will be initialized to 1, the variable min_product will be initialized to 1, and the variable max_ will be initialized to 0.
  • We will run a for loop from 0 to N.
  • If the current integer is greater than 0, then:
    • We will set the max_product equal to min_product * array[i].
    • And, we will set min_product equal to the minimum value of the min_product * array[i] and 1.
    • We will set the Boolean variable as 1
  • Else if the integer of the current iteration is equal to zero
    • We will set both the variables max_product and min_product equal to one.
  • Else
    • We will set max_product equal to the maximum value of min_product * array[i] and 1
    • We will set the variable min_product equal to max_product * array[i]
  • If the value of max_product is larger than that of the variable max_, then we will update max_.
  • If the Boolean flag is set to zero, then we will return zero
  • If the max_ has value one, i.e., all array integers are negative, then we will return the maximum integer in the given array
  • At last, we will return max_.

Here is the implementation of this approach.

Code

Output:

The maximum product of one of the subarrays is: 24156

Time Complexity: The time complexity of this method is linear, i.e., O(N), where N is the number of elements in the array.

Auxiliary Space: The space complexity of this method is constant, i.e., O(1).

Efficient Approach

We will follow the algorithm below to solve the issue:

The preceding approach assumes that the provided array will always have a positive result, which is incorrect when the array solely includes negative values, such as (0, 0, -20, 0), (0, 0, 0), etc. The updated solution is comparable to the Largest Sum Contiguous Subarray Problem, which uses the largest sum.

Here is the step-by-step approach to this method:

  • Here we will initialize three variables called max_, max_end_here & min_end_here
  • For every iteration, the maximum product ending at that the index of that iteration will be a maximum of (array[i], max_end_here * array[i], min_end_here[i] * array[i])
  • Likewise, the minimum product ending at that index will be the minimum value of these three variables.
  • Hence, we will get the final maximum product value of a subarray of the given array.

Here is the implementation in Python:

Code

Output:

The maximum product of one of the subarrays is: 24156

Time Complexity: The time complexity of this approach is linear since we are traversing the array of integers. Hence, the time complexity is O(N), where N is the number of elements in the array.

Auxiliary Space:This approach will take O(1) extra space since we have only created variables.

Efficient Approach

We'll use a straightforward strategy that involves traversing and multiplying the components, and if our result is higher than the product value that has already been saved, we'll store it instead. If "0" appears, set the products of all previous items to 1 since the following element will begin a new subarray.

But there is a problem with this method.

When our array contains an unusually high number of negative entries, the problem will arise. Then, to have an even number of negative integers and a positive end product, we must reject every negative component. Now that we think about subarray, we can't reject one unfavorable component. Either the initial negative component or the last negative component must be rejected.

However, only the final negative component can be discarded if we navigate from the beginning, and the initial negative component could be rejected if we navigate from the last. Therefore, we will navigate from both ends and both traversals. We will select the result from the navigation that will provide the maximum amount of product subarray.

We will reject that negative element whose rejection will give us the maximum product's subarray.

Code

Output:

The maximum product of one of the subarrays is: 24156

Time Complexity: The time complexity of this approach is linear since we are traversing the array of integers. Hence, the time complexity is O(N), where N is the number of elements in the array.

Auxiliary Space: This approach will take O(1) extra space since we have only created variables.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA