Tutorialspoint
Problem
Solution
Submissions

Maximum Product of a Subarray

Certification: Intermediate Level Accuracy: 0% Submissions: 0 Points: 10

Given an integer array nums, find a contiguous non-empty subarray within the array that has the largest product, and return the product.

Example 1
  • Input: nums = [2, 3, -2, 4]
  • Output: 6
  • Explanation:
    • Step 1: Consider all contiguous subarrays of nums [2, 3, -2, 4].
    • Step 2: Calculate the product of elements in each subarray.
    • Step 3: Subarray [2] has product 2.
    • Step 4: Subarray [3] has product 3.
    • Step 5: Subarray [-2] has product -2.
    • Step 6: Subarray [4] has product 4.
    • Step 7: Subarray [2, 3] has product 6.
    • Step 8: Subarray [3, -2] has product -6.
    • Step 9: Subarray [-2, 4] has product -8.
    • Step 10: Subarray [2, 3, -2] has product -12.
    • Step 11: Subarray [3, -2, 4] has product -24.
    • Step 12: Subarray [2, 3, -2, 4] has product -48.
    • Step 13: The maximum product is 6 from the subarray [2, 3].
Example 2
  • Input: nums = [-2, 0, -1]
  • Output: 0
  • Explanation:
    • Step 1: Consider all contiguous subarrays of nums [-2, 0, -1].
    • Step 2: Calculate the product of elements in each subarray.
    • Step 3: Subarray [-2] has product -2.
    • Step 4: Subarray [0] has product 0.
    • Step 5: Subarray [-1] has product -1.
    • Step 6: Subarray [-2, 0] has product 0.
    • Step 7: Subarray [0, -1] has product 0.
    • Step 8: Subarray [-2, 0, -1] has product 0.
    • Step 9: The maximum product is 0 from the subarrays containing 0.
Constraints
  • 1 ≤ nums.length ≤ 2 × 10^4
  • -10 ≤ nums[i] ≤ 10
  • The product of any subarray will not exceed the range of a 32-bit signed integer
  • Time Complexity: O(n)
  • Space Complexity: O(1)
ArraysFacebookeBay
Editorial

Login to view the detailed solution and explanation for this problem.

My Submissions
All Solutions
Lang Status Date Code
You do not have any submissions for this problem.
User Lang Status Date Code
No submissions found.

Please Login to continue
Solve Problems

 
 
 
Output Window

Don't have an account? Register

Solution Hints

  • Consider both maximum and minimum products at each position to handle negative numbers
  • A negative number can turn the minimum product (which could be very negative) into a large positive product
  • Reset your calculations when encountering zeros
  • Keep track of the global maximum product seen so far
  • Consider using dynamic programming to solve this problem efficiently

Steps to solve by this approach:

 Step 1: Initialize variables to track maximum product (max_so_far), minimum product (min_so_far), and result

 Step 2: Iterate through the array starting from the second element
 Step 3: For each element, calculate possible new maximum and minimum products considering current value and previous products
 Step 4: Update max_so_far and min_so_far based on these calculations
 Step 5: Update the result if the new max_so_far is greater than the current result

Submitted Code :