ProCodebaseProCodebase
  • ModusA Growth OS for your business, on WhatsAppKiosqSell more. Chase less.AI InterviewerAutomated screening & AI-led interviewsXperto AIAI prep companion for candidatesAI Tools HubResume builder, learning paths & more
  • Pre-Vetted DevelopersScreened, scored and ready to interviewAI-Native DevelopersSenior engineers on an hourly basis
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
  • Services
  • Features
  • Jobs
  • FAQs
Sign inBook a demo
ProCodebaseProCodebase

ProCodebase Technologies builds AI products for hiring and growth, and ships software for clients as a technical consultancy. We source, screen and deliver pre-vetted developers — so you only interview high-signal candidates.

Products

  • Modus
  • Kiosq
  • AI Interviewer
  • Xperto AI
  • AI Tools Hub

Hire & build

  • Pre-Vetted Developers
  • AI-Native Developers
  • Technical Consultancy
  • MVP Development
  • Features

Resources

  • Articles
  • Topics
  • Certifications
  • Collections
  • Jobs

Company

  • About Us
  • Contact Us
  • Book a Demo
  • FAQs

© 2026 ProCodebase Technologies. All rights reserved.

  • Privacy Policy
  • Terms & Conditions
  • Refund & Cancellation

Level Up Your Skills with Xperto-AI

A multi-AI agent platform that helps you level up your development skills and ace your interview preparation to secure your dream job.

Launch Xperto-AI

Finding the Maximum Product Subarray

author
Generated by
ProCodebase AI

15/11/2024

Dynamic Programming

Sign in to read full article

The Maximum Product Subarray problem is a classic algorithmic challenge often encountered in programming interviews and competitive coding. The objective is simple: given an array of integers (which can include both negative and positive values), the goal is to find the contiguous subarray that yields the maximum product.

Problem Statement

Given an array nums, your task is to find the contiguous subarray within nums (containing at least one number) which has the largest product and return that product.

Example:
Input: [-2, 3, -4]
Output: 24
Explanation: The contiguous subarray [3, -4] has the maximum product, which equals 3 * -4 = -12, but taking into account the entire array results in a maximum product of -2 * 3 * -4 = 24.

Understanding the Challenge

While it may be tempting to use a brute force approach to check every possible subarray, this would result in a time complexity of O(n^2). Instead, we need a more efficient solution, ideally O(n).

The key to tackling this problem lies in recognizing that the product of two negative numbers is positive. Thus, maintaining two values during our traversal—one for the maximum product and another for the minimum product—is crucial, as we can encounter a situation where a small negative number turns a large negative product into a larger positive product.

Dynamic Programming Approach

To implement a dynamic programming solution, we can use a single pass through the array to calculate the maximum and minimum product at each step.

  1. Initialization:
    Start with variables to track the maximum product (max_prod), the minimum product (min_prod), and the result (result).

    max_prod = min_prod = result = nums[0]
  2. Iterate through the array:
    For each number in the array from the second element onward:

    • If the current number is negative, swap max_prod and min_prod.
    • Update the max_prod to be the maximum of the current number and the product of max_prod with the current number.
    • Update the min_prod in a similar manner.
    • The resultant maximum product at each position should also be updated.
  3. Return the result
    After traversing the array, the resultant maximum product will hold the maximum product of any subarray.

Here’s the implementation in Python:

def max_product_subarray(nums): max_prod = min_prod = result = nums[0] for num in nums[1:]: if num < 0: max_prod, min_prod = min_prod, max_prod max_prod = max(num, max_prod * num) min_prod = min(num, min_prod * num) result = max(result, max_prod) return result

Walkthrough of the Example

Let’s illustrate the operation of the algorithm using the example array [-2, 3, -4].

  • Initialization: max_prod = -2, min_prod = -2, result = -2

  • Iteration 1 (num = 3):

    • 3 is positive, so we continue.
    • max_prod = max(3, -2 * 3) = 3
    • min_prod = min(3, -2 * 3) = -6
    • Result update: result = max(-2, 3) = 3
  • Iteration 2 (num = -4):

    • -4 is negative, so swap max_prod and min_prod: now max_prod = -6, min_prod = 3
    • max_prod = max(-4, -6 * -4) = 24
    • min_prod = min(-4, 3 * -4) = -12
    • Result update: result = max(3, 24) = 24

Finally, we return 24 as the maximum product subarray.

Complexity Analysis

  • Time Complexity: O(n) - As we only traverse the array once.
  • Space Complexity: O(1) - We are using a constant amount of additional space for our variables.

With these techniques and insights, tackling the Maximum Product Subarray becomes a streamlined process, perfectly aligning with efficient algorithm design principles. As you practice more problems like this, the underlying patterns of dynamic programming will become increasingly clear and intuitive.

Popular tags

Dynamic ProgrammingDSAMaximum Product Subarray

Share now!

Like & bookmark

Related collections

  • Advanced Dynamic Programming Interview Questions

    15/11/2024 · DSA

  • DSA Mastery for Interviews

    23/09/2024 · DSA

  • Mastering Arrays : The Basic Data Structure

    06/12/2024 · DSA

  • Advanced Recursion and Backtracking Problems Using Java

    13/10/2024 · DSA

  • Mastering Bit Manipulation: Unlocking Binary Power

    08/12/2024 · DSA

Related articles

  • Finding the Maximum Subarray Sum with Kadane’s Algorithm

    06/12/2024 · DSA

  • The Knapsack Problem

    15/11/2024 · DSA

  • Generate All Permutations of a String

    13/10/2024 · DSA

  • Master NOT Operator in DSA

    08/12/2024 · DSA

  • Understanding Trie Data Structure

    03/09/2024 · DSA

  • Understanding Bridges and Articulation Points in Graphs

    16/11/2024 · DSA

  • Understanding the Integer Partition Problem

    15/11/2024 · DSA

Popular category

  • Python
  • Generative AI
  • Machine Learning
  • ReactJS
  • System Design