Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Maximum Product Subarray Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Maximum Product Subarray | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

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

Naive Approach

Here, we can iterate over all the possible subarrays in the array, and calculate the maximum product out of all the subarrays. We can do this by fixing 2 indexes i and j, and iterating over the subarray starting at i and ending at j.

Analysis

  • Time Complexity: O(n3)
  • Space Complexity: O(1)

Implementation

C++
int maxProduct(vector<int> A) {
   int maxProductValue = INT_MIN;
   for(int i = 0; i < A.size(); i++) {
       for(int j = i; j < A.size(); j++) {
           int product = 1;
           for(int k = i; k <= j; k++) {
               product *= A[k];
           }
           maxProductValue = max(maxProductValue, product);
       }
   }
   return maxProductValue;
}
Java
class Solution {
   int maxProduct (int[] A) {
       int maxProductValue = Integer.MIN_VALUE;
       for(int i = 0; i < A.length; i++) {
           for(int j = i; j < A.length; j++) {
               int product = 1;
               for(int k = i; k <= j; k++) {
                   product *= A[k];
               }
               maxProductValue = Math.max(maxProductValue, product);
           }
       }
       return maxProductValue;
   }
}

Optimal Approach

If all the numbers in the array were positive, the answer will definitely be the product of the whole array.

If we now consider the case, when there are no negative numbers in the array, we can approach it as soon as we encounter a 0, we reset our product to 1 and keep taking the maximum product in each iteration.

With the introduction of negative numbers, we now have to keep track of the maximum positive product and the maximum negative product. When we encounter a negative number, the maximum negative product is taken into consideration.

Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

Implementation

C++
int maxProduct(vector<int> A) {
   if(A.size() == 1) {
       return A[0];
   }
   int currMax = A[0], currMin = A[0], maxProductValue = A[0];
   for(int i = 1; i < A.size(); i++) {
       if(A[i] >= 0) {
           currMax = max(A[i], A[i] * currMax);
           currMin = min(A[i], A[i] * currMin);
           maxProductValue = max(maxProductValue, currMax);
       } else {
           swap(currMin, currMax);
           currMax = max(A[i], A[i] * currMax);
           currMin = min(A[i], A[i] * currMin);
           maxProductValue = max(maxProductValue, currMax);
       }
   }
   return maxProductValue;
}
Java
class Solution {
   int maxProduct (int[] A) {
       if(A.length == 1) {
           return A[0];
       }
       int currMax = A[0], currMin = A[0], maxProductValue = A[0];
       for(int i = 1; i < A.length; i++) {
           if(A[i] >= 0) {
               currMax = Math.max(A[i], A[i] * currMax);
               currMin = Math.min(A[i], A[i] * currMin);
               maxProductValue = Math.max(maxProductValue, currMax);
           } else {
               int temp = currMax;
               currMax = currMin;
               currMin = temp;
               currMax = Math.max(A[i], A[i] * currMax);
               currMin = Math.min(A[i], A[i] * currMin);
               maxProductValue = Math.max(maxProductValue, currMax);
           }
       }
       return maxProductValue;
   }
}
Related Content
Best Time to Buy and Sell Stocks
Best Time to Buy and Sell Stocks II
Best Time to Buy and Sell Stock III
Best Time to Buy and Sell Stock IV
Climbing Stairs
Coin Change
Collect Jewels
Decode Ways
Edit Distance (Levenshtein Distance)
Minimum Egg Drops
Longest Common Subsequence (LCS)
Longest Increasing Subsequence (LIS)
Longest Palindromic Substring (LPS)
Maximum path sum in matrix
Maximum Sum Increasing Subsequence
Palindrome Partitioning
Palindrome Partitioning 2
Regular Expression Matching
Rod Cutting
Subset Sum
Subset Sum 2
Trapping Rain Water
Unique Paths
Wildcard Matching
Word Break
Word Break - II
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet