0% found this document useful (0 votes)
6 views

L20 - Max Product Subarray

The document discusses different approaches to find the maximum product subarray in a given integer array that can contain both positive and negative numbers. It first explains what a subarray and subsequence are. It then describes a naive approach of calculating the product of all contiguous subarrays and returning the maximum. This has a time complexity of O(N^2). It further explains an optimized approach using Kadane's algorithm that runs in O(N) time by maintaining maximum and minimum ending products at each index.

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

L20 - Max Product Subarray

The document discusses different approaches to find the maximum product subarray in a given integer array that can contain both positive and negative numbers. It first explains what a subarray and subsequence are. It then describes a naive approach of calculating the product of all contiguous subarrays and returning the maximum. This has a time complexity of O(N^2). It further explains an optimized approach using Kadane's algorithm that runs in O(N) time by maintaining maximum and minimum ending products at each index.

Uploaded by

Siddharth Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Max product sub array

Introduction
✔ Given an array that contains both positive and
negative integers, the task is to find the product of the
maximum product sub array.
✔ Subsequence: A subsequence is a sequence that can
be derived from another sequence by zero or more
elements, without changing the order of the
remaining elements.
✔ Sub array: A sub array is a contiguous part of array. An
array that is inside another array.
A Subsequence cannot be a sub array
✔ For instance, let arr = [1,2,3,4,5]
✔ Examples for Subarrays: [1], [1,2], [2,3,4] etc
✔ Examples for Subsequences: [1], [1,3], [1,2,3], [2,3,4]
Maximum Product Subarray by Traverse Over Every
Contiguous Subarray:

✔ The idea is to traverse over every contiguous sub array,


✔ find the product of each of these sub arrays and return
✔ the maximum product from these results.
✔ Follow the below steps to solve the problem:
✔ Run a nested for loop to generate every sub array
✔ Calculate the product of elements in the current sub array
✔ Return the maximum of these products calculated from
the sub arrays
import java.io.*;
class Max {
static int maxSubarrayProduct(int arr[])
{
// Initializing result
int result = arr[0];
int n = arr.length;
for (int i = 0; i < n; i++) {
int mul = arr[i];
// traversing in current subarray
for (int j = i + 1; j < n; j++) {
result = Math.max(result, mul);
mul *= arr[j];
}
result = Math.max(result, mul);
}
return result;
}

// Driver Code
public static void main(String[] args)
{
int arr[] = { 1, -2, -3, 0, 7, -8, -2 };
System.out.println("Maximum Sub array product is "
+
maxSubarrayProduct(arr));
}
}
Output : Maximum Sub array product is 112

Time Complexity: O(N2)


Auxiliary Space: O(1)

Maximum Product Sub array using Kadane’s Algorithm:

✔ The idea is to use Kadane’s algorithm and maintain 3 variables max_so_far, max_ending_here
& min_ending_here. Iterate the indices 0 to N-1 and update the variables such that.

✔ max_ending_here = maximum(arr[i], max_ending_here * arr[i], min_ending_here[i]*arr[i])

✔ min_ending_here = minimum(arr[i], max_ending_here * arr[i], min_ending_here[i]*arr[i])

✔ update the max_so_far with the maximum value for each index.

✔ return max_so_far as the result.

✔ Time Complexity: O(N)


Auxiliary Space: O(1)
class max {
static int maxSubarrayProduct(int arr[], int n)
{

int max_ending_here = arr[0];

int min_ending_here = arr[0];

int max_so_far = arr[0];

for (int i = 1; i < n; i++) {


int temp = Math.max(
Math.max(arr[i], arr[i] * max_ending_here),
arr[i] * min_ending_here);
min_ending_here = Math.min(
Math.min(arr[i], arr[i] * max_ending_here),
arr[i] * min_ending_here);
max_ending_here = temp;
max_so_far
= Math.max(max_so_far, max_ending_here);
}

return max_so_far;
}

// Driver code
public static void main(String args[])
{
int[] arr = { 1, -2, -3, 0, 7, -8, -2 };
int n = arr.length;
System.out.printf("Maximum Sub array product is %d",
THANK YOU

You might also like