Search⌘ K
AI Features

Maximum Product Subarray

Explore how to identify the maximum product subarray within a given integer array by applying dynamic programming methods. Understand problem constraints and implement solutions that optimize subarray product calculations using memoization and tabulation strategies.

Statement

Given an integer array, nums, find a subarray that has the largest product, and return the product.

Constraints:

  • 11\leqnums.length 103\leq10^3

  • 10-10 \leqnums[i] 10\leq 10

  • The product of any prefix or suffix of nums is guaranteed to fit in a 32bit32-bit integer.

Example

Understand the problem

Let’s take a moment to make sure you’ve correctly understood the problem. The quiz below helps you check if you’re solving the correct problem:

Maximum Product Subarray

1.

Given the following array, what is the maximum product we can obtain from a subarray?

nums = [1, 2, 3, 0, 4]

A.

4

B.

6

C.

8

D.

2


1 / 3

Figure it out

We have a game for you to play. Rearrange the logical building blocks to develop a clearer understanding of how to solve this problem.

Sequence - Vertical
Drag and drop the cards to rearrange them in the correct sequence.

1
2
3
4
5

Try it yourself

Implement your solution in the following coding playground.

Need a nudge?

Explore these hints—each one is designed to guide you a step closer to the solution.

Java
usercode > MaxProduct.java
import java.util.*;
public class MaxProduct{
public static int maxProduct(int [] nums) {
// Replace this placeholder return statement with your code
return -1;
}
}
Maximum Product Subarray