Given an integer array nums
, find a subarray that has the largest product, and return the product.
The test cases are generated so that the answer will fit in a 32-bit integer.
Example 1:
Input: nums = [2,3,-2,4] Output: 6 Explanation: [2,3] has the largest product 6.
Example 2:
Input: nums = [-2,0,-1] Output: 0 Explanation: The result cannot be 2, because [-2,-1] is not a subarray.
Constraints:
1 <= nums.length <= 2 * 104
-10 <= nums[i] <= 10
nums
is guaranteed to fit in a 32-bit integer.When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The problem asks us to find the largest product from a continuous section of numbers in a given list. A brute force method involves checking every possible section to find the one with the biggest product.
Here's how the algorithm would work step-by-step:
def max_product_subarray_brute_force(numbers):
max_product = numbers[0]
# Iterate through the start indices
for start_index in range(len(numbers)):
current_product = 1
# Iterate through the end indices for each start
for end_index in range(start_index, len(numbers)):
current_product *= numbers[end_index]
# We must keep track of largest product
if current_product > max_product:
max_product = current_product
return max_product
The key idea is to keep track of the largest and smallest product seen so far as you move through the numbers. This is important because a negative number can turn a small negative into a large positive, and vice versa.
Here's how the algorithm would work step-by-step:
def max_product_subarray(numbers):
maximum_product = numbers[0]
maximum_so_far = numbers[0]
minimum_so_far = numbers[0]
for i in range(1, len(numbers)):
current_number = numbers[i]
# Need to find max of three possible products.
temp_max = max(current_number, current_number * maximum_so_far, current_number * minimum_so_far)
# Need to find min of three possible products.
minimum_so_far = min(current_number, current_number * maximum_so_far, current_number * minimum_so_far)
maximum_so_far = temp_max
# Keep track of overall maximum product.
maximum_product = max(maximum_product, maximum_so_far)
return maximum_product
Case | How to Handle |
---|---|
Empty input array | Return 0 or 1, depending on the problem's specification (usually 1 as the product of an empty set is often defined as 1). |
Array with a single element | Return that single element as the maximum product subarray. |
Array with all zeros | Return 0 as the product of any subarray containing zero will be zero. |
Array with all negative numbers | The largest product will be either the single largest element if the array has odd length or the product of all numbers if it has even length. |
Array with mixed positive and negative numbers, including zeros | The solution needs to track both the maximum and minimum product ending at each index due to negative number multiplication. |
Integer overflow with large numbers | Use a larger data type like long long or consider handling overflows by checking if intermediate products exceed the maximum representable value. |
Array contains only the minimum integer value | Handle the potential overflow when multiplying the minimum integer value with itself or another negative number. |
Very long input array | The dynamic programming approach ensures linear time complexity, thus scaling well with large input sizes. |