Python/data_structures/arrays/max_product_subarray.py
2024-11-01 18:05:58 +03:00

19 lines
541 B
Python

"""
Given an integer array, the task is to find the maximum product of any subarray.
Input: arr[] = {-1, -3, -10, 0, 60}
Output: 60
Explanation: The subarray with maximum product is {60}.
"""
def max_product(arr):
result = 0
for x in range(len(arr)):
current_product = 1
for y in range(x, len(arr)):
current_product *= arr[y]
result = max(result, current_product)
return result
# Test the function with the provided input
print(max_product([-1, -3, -10, 0, 60])) # Expected output: 60