Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

TEST TIME ON BLOCK SWAP ALGORITHM

URL: https://forms.gle/hMidTZ74S5Wg1uvS8
QR CODE:
MAXIMUM PRODUCT
SUBARRAY
Maximum Product Subarray

Introduction
The maximum product subarray problem involves finding a contiguous
subarray within an array of integers that has the largest product.
Subarray vs Subsequence
Subarray: A subarray is a contiguous section of elements taken from an
array. It means that the elements are selected in a sequence, without
skipping any elements in between.
For example, in the array [1, 2, 3, 4], a subarray could be [2, 3, 4],
[1, 2], [3], or even the entire array itself.
Maximum Product Subarray

“A Subsequence cannot be a subarray”

Subsequence: A subsequence, on the other hand, is a subset of


elements from a sequence, where the elements may not
necessarily be adjacent or in the original order. It means that
elements can be skipped or selected in any order.
For example, in the sequence [1, 2, 3, 4], a subsequence could
be [2, 4], [1, 3, 4], [2, 3], or even the empty sequence [].
Maximum Product Subarray

“Finding the contiguous subarray with the largest product”

Here is a step-by-step approach to solving this problem:


1.Initialize two variables, maxProduct and minProduct, to store
the maximum and minimum product found so far, respectively. Set
both variables to the first element of the array.
2.Initialize a variable result to store the maximum product
subarray value. Set it to the same value as maxProduct.
3.Iterate through the array starting from the second element.
Maximum Product Subarray

4.For each element, calculate the new maxProduct and minProduct by


considering the maximum of four cases:
a. The current element itself.
b. The maximum product obtained by multiplying the current
element with the maxProduct.
c. The minimum product obtained by multiplying the current
element with the minProduct.
d. The current element multiplied with the minProduct if it
becomes the new maxProduct (in case the previous maxProduct was
negative).
Maximum Product Subarray

5.Update the result variable with the maximum of result and

maxProduct.
6.Repeat steps 3 to 5 until all elements of the array have
been processed.
7.Finally, return the value of result, which represents the
maximum product subarray.
Maximum Product Subarray

Time and space complexity

time complexity O(n)


where n is the number of elements in the array.
space complexity O(1)
No extra data structure is used for computation
Maximum Product Subarray

Program

Sample input 1:
{2,-2,3,4}
Max Product= 12

Sample input 2:
{4,1,0,9,8,-1,9,9}
Max Product= 81
Maximum Product Subarray-A1
import java.util.*; int prefix=1;
import java.lang.*; int suffix=1;
public class Main int ans=1;
{ for(int i=0; i<n;i++){
public static void main(String[] args) if(prefix==0)
{ prefix=1;
Scanner s = new if(suffix==0)
Scanner(System.in); suffix=1;
System.out.println("Enter size of the prefix=prefix*arr[i];
array"); suffix=suffix*arr[n-i-1];
int n = s.nextInt(); ans=Math.max(ans,Math.max(prefix,suff
int[] arr = new int[n]; ix));
System.out.println("Enter elements of }
the array"); System.out.println(ans);
for (int i = 0; i < n; i++){ }}
arr[i] = s.nextInt();
}
Maximum Product Subarray-A2
import java.io.*;
class Main{ max_ending_here = temp;
static int maxSubarrayProduct(int arr[], int max_so_far= Math.max(max_so_far,
n) max_ending_here);
{ }
int max_ending_here = arr[0];
int min_ending_here = arr[0]; return max_so_far;
int max_so_far = arr[0]; }
for (int i = 1; i < n; i++) { public static void main(String
args[])
int temp = Math.max( {
Math.max(arr[i], arr[i] * max_ending_here), int[] arr = { 1, -2, -3,
arr[i] * 0, 7, -8, -2 };
min_ending_here); int n = arr.length;
System.out.printf("Maximum Sub array
min_ending_here = Math.min( product is %d",maxSubarrayProduct(arr, n));
Math.min(arr[i], arr[i] * max_ending_here), }
}
arr[i]*min_ending_here);
Maximum Product Subarray-A3
import java.util.*; maxSoFar = tempMax;
public class Main { result = Math.max(maxSoFar, result);
public static int }
maxProduct(List<Integer> nums) { return result;
if (nums.size() == 0) }
return 0; public static void main(String[] args)
int maxSoFar = nums.get(0); {
int minSoFar = nums.get(0); List<Integer> nums = Arrays.asList(2,
int result = maxSoFar; 5,-15);
for (int i = 1; i < nums.size(); int maxProduct = maxProduct(nums);
i++) { System.out.println("Max Product: "
int curr = nums.get(i); + maxProduct);
int tempMax = Math.max(curr, }
Math.max(maxSoFar * curr, minSoFar * }
curr));
minSoFar = Math.min(curr,
Math.min(maxSoFar * curr, minSoFar *
curr));
Interview questions

Explain the Maximum Product Subarray problem.

The Maximum Product Subarray problem involves finding a


contiguous subarray within an array that has the
largest product of its elements.
Interview questions

How would you approach solving the Maximum Product Subarray


problem?

One common approach is to use dynamic programming. We can


keep track of both the maximum and minimum product
subarrays at each index while iterating through the array.
By considering the maximum of four cases for each element,
we update the maximum and minimum subarray products.
Finally, we return the maximum product found.
Interview questions

What is the time complexity of the Maximum Product Subarray


algorithm?

The time complexity of the algorithm is O(n), where n is


the size of the input array. This is because we iterate
through the array only once, performing constant time
operations at each index.
Interview questions

How can we handle edge cases in the Maximum Product Subarray


problem?

One possible edge case is an empty array. In such


cases, we can return 0 as there are no elements to
consider.
Another edge case is when the array has only one
element. In this case, the maximum product subarray
would be that single element.
/ethnuscodemithra Ethnus Codemithra /ethnus /code_mithra

https://learn.codemithra.com

codemithra@ethnus.com +91 7815 095 095 +91 9019 921 340

You might also like