Maximum Product Subarray - aloalgo

Maximum Product Subarray

Medium

You are given an integer array nums. Your task is to find the contiguous subarray within the array (containing at least one number) that has the largest product and return that product.

A contiguous subarray is a sequence of adjacent elements.

Example 1

Input
[2, 3, -2, 4]
Output
6
Explanation:

The subarray [2,3] has the largest product 6.

Example 2

Input
[-2, 0, -1]
Output
0
Explanation:

The subarray [0] has the largest product 0. Note that the subarray [-2,-1] has product 2, but it's not contiguous with 0, and [0] is a valid contiguous subarray.

Example 3

Input
[-2, 3, -4]
Output
24
Explanation:

The subarray [-2,3,-4] has the largest product 24.

Loading...
Input
[2, 3, -2, 4]
Output
6

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!