Professional Documents
Culture Documents
WINSEM2023-24 MSTS601L TH CH2023240503480 Reference Material I 29-02-2024 Max Product Subarray
WINSEM2023-24 MSTS601L TH CH2023240503480 Reference Material I 29-02-2024 Max Product Subarray
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
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
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
https://learn.codemithra.com