Maximum Product Subarray

Advanced

Maximum Product Subarray

Hello young coders! Are you ready to dive into the world of arrays? Today, we're going to find the maximum product of a subarray. That means within a list of numbers, we want to figure out the greatest product we can get by multiplying numbers that are next to each other.

Think of it like lining up a row of dominoes and knocking them over; you want to knock over as many as possible to get the best result!

The Challenge

Your task is to write a function called max_product_subarray that receives a list of integers. Your function should return the maximum product that can be obtained from a contiguous subsequence of the array.

Here's an example to understand better:

max_product_subarray([2, 3, -2, 4])

should return 6 because 2 * 3 = 6 is the maximum product for a contiguous subarray.

Another example:

max_product_subarray([-2, 0, -1])

should return 0 because the subarray [0] gives the maximum product.

Let's Conquer This!

  • Go through the list and try to check different products.
  • Remember! Negative numbers can flip the sign of your product, so be careful!

Happy Coding! 🎉

Geek
Inputs

Arg 1:

[2,3,-2,4]
Expected Output
6
Actual Output