Maximum Product SubarrayFind the largest product of any subarray of an array containing positive and negative integers. ExamplesInput: 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 - 1Follow 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:
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 - 2Below 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:
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 ApproachWe 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 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 ApproachWe'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. |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India