BackmediumArraysAdobe

Maximum Subarray Product Solution

Problem Statement

You are given an array of integers values representing commodity multipliers. Write a function to find the maximum product of a subarray within the given array, where the subarray must contain at least one element.

Example 1
Input
[2,3,-2,4]
Output
6

Explanation: Step-by-step: We initialize maxProduct and minProduct to the first element of the array, which is 2. Then, we iterate through the array. For the second element 3, we update maxProduct to be the maximum of 2 * 3 and 3, which is 6. For the third element -2, we update maxProduct to be the maximum of 6 and -2 * 6, which is 6. For the fourth element 4, we update maxProduct to be the maximum of 6 and 4 * 6, which is 24. However, we also need to consider the case where the maxProduct becomes negative. For the second element 3, we update minProduct to be the minimum of 2 * 3 and 3, which is 6. For the third element -2, we update minProduct to be the minimum of 6 and -2 * 6, which is -12. For the fourth element 4, we update minProduct to be the minimum of -12 and 4 * -12, which is -48. Finally, we return maxProduct, which is 24.

Example 2
Input
[0,0,0]
Output
0

Explanation: Step-by-step: We initialize maxProduct to 0. Then, we iterate through the array. For the first element 0, we update maxProduct to be the maximum of 0 * 0 and 0, which is 0. For the second element 0, we update maxProduct to be the maximum of 0 and 0 * 0, which is 0. For the third element 0, we update maxProduct to be the maximum of 0 and 0 * 0, which is 0. Finally, we return maxProduct, which is 0.

Constraints

  • The input array will have at least one element.
  • The input array will contain only integers between -100 and 100.
Live Compiler1 Free Run Available
Loading Editor...
Test Cases & Output
Click "Run" to test your 1 free compile trial!

šŸš€ Practice this problem

Run code, get AI hints & track streak

Sign Up Free

Maximum Subarray Product — Problem Statement & Solution Guide

ArraysMediumbasic-concepts
TimeO(n)
|
SpaceO(1)

Problem Description

You are given an array of integers values representing commodity multipliers. Write a function to find the maximum product of a subarray within the given array, where the subarray must contain at least one element.

Examples

Example 1

Input

[2,3,-2,4]

Output

6

Explanation: Step-by-step: We initialize maxProduct and minProduct to the first element of the array, which is 2. Then, we iterate through the array. For the second element 3, we update maxProduct to be the maximum of 2 * 3 and 3, which is 6. For the third element -2, we update maxProduct to be the maximum of 6 and -2 * 6, which is 6. For the fourth element 4, we update maxProduct to be the maximum of 6 and 4 * 6, which is 24. However, we also need to consider the case where the maxProduct becomes negative. For the second element 3, we update minProduct to be the minimum of 2 * 3 and 3, which is 6. For the third element -2, we update minProduct to be the minimum of 6 and -2 * 6, which is -12. For the fourth element 4, we update minProduct to be the minimum of -12 and 4 * -12, which is -48. Finally, we return maxProduct, which is 24.

Example 2

Input

[0,0,0]

Output

0

Explanation: Step-by-step: We initialize maxProduct to 0. Then, we iterate through the array. For the first element 0, we update maxProduct to be the maximum of 0 * 0 and 0, which is 0. For the second element 0, we update maxProduct to be the maximum of 0 and 0 * 0, which is 0. For the third element 0, we update maxProduct to be the maximum of 0 and 0 * 0, which is 0. Finally, we return maxProduct, which is 0.

Constraints

  • The input array will have at least one element.
  • The input array will contain only integers between -100 and 100.

Optimal Approach & Strategy

The optimal approach uses dynamic programming to keep track of the maximum and minimum product of subarrays ending at each position, allowing it to handle negative numbers and find the maximum product in linear time.

Brute Force Approach

A naive approach would be to generate all possible subarrays, calculate their product, and keep track of the maximum product found. This would have a time complexity of O(n²) due to the nested loops. However, this approach is inefficient for large inputs.

Verified Code Solutions

JavaScript Solution
Time: O(n)
function maxSubarrayProduct(values) {
  if (values.length === 0) return 0;
  let maxProduct = values[0];
  let minProduct = values[0];
  let result = values[0];

  for (let i = 1; i < values.length; i++) {
    const current = values[i];
    maxProduct = Math.max(Math.max(maxProduct * current, current), maxProduct * minProduct);
    minProduct = Math.min(Math.min(minProduct * current, current), maxProduct * current);
    result = Math.max(result, maxProduct);
  }

  return result;
}

Asked in Top Tech Interviews

Adobe

Solve in Interative Editor

Ready to test your code? Open our built-in compiler, run custom test suites, and see detailed complexity analysis reports instantly.