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] <= 10nums 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:
Imagine you have a row of numbers and you want to find a continuous group of them that multiplies together to make the biggest possible result. The brute force approach involves looking at every single possible continuous group, multiplying them out, and seeing which one wins.
Here's how the algorithm would work step-by-step:
def max_product_subarray(input_numbers):
# Handle edge case where the input list is completely empty
if not input_numbers:
return 0
highest_product_found = input_numbers[0]
# Iterate through every possible starting position in the array
for start_index in range(len(input_numbers)):
current_group_product = 1
# Extend the continuous group one element at a time and multiply
for end_index in range(start_index, len(input_numbers)):
current_group_product *= input_numbers[end_index]
# Update our tracked maximum if the current group product is larger
if current_group_product > highest_product_found:
highest_product_found = current_group_product
return highest_product_foundTo find the group of numbers next to each other that multiplies to the largest possible result, we cannot just rely on keeping track of the biggest numbers seen so far. Because multiplying two negative numbers creates a positive number, we must also track the smallest negative results as we move along, just in case they suddenly flip into massive positives.
Here's how the algorithm would work step-by-step:
def max_product(array_of_numbers):
current_max_product = array_of_numbers[0]
current_min_product = array_of_numbers[0]
overall_max_product = array_of_numbers[0]
# Negatives can flip to positives, so tracking both extremes is required.
for next_number in array_of_numbers[1:]:
candidate_products = (
next_number,
next_number * current_max_product,
next_number * current_min_product
)
current_max_product = max(candidate_products)
current_min_product = min(candidate_products)
# The global maximum must be updated at each step to capture the best result.
overall_max_product = max(overall_max_product, current_max_product)
return overall_max_product| Case | How to Handle |
|---|---|
| Array of size one | Return the single element immediately since a single-element subarray is contiguous and non-empty. |
| Array containing a single zero | Return zero because the only possible non-empty contiguous subarray product is zero. |
| Array with alternating negative numbers | Track both the maximum and minimum product at each step so that a negative minimum can flip into a large positive product upon encountering the next negative number. |
| Array with zeros interspersed among positive and negative numbers | Reset the running maximum and minimum products to one when encountering a zero, effectively starting a new subarray search. |
| All elements are negative numbers with an odd length | Track minimum products to ensure the algorithm correctly identifies the maximum possible product formed by all negative numbers except the smallest magnitude one. |
| Integer overflow from extremely large positive or negative products | Use 64-bit integers or guard against overflow depending on the programming language constraints to prevent wrapping around incorrect values. |
| Maximum-sized inputs up to the problem constraints limit | Use an O(n) time and O(1) space dynamic programming approach to ensure the solution executes within time limits and memory constraints. |