#C6581. Maximum Product Subarray
Maximum Product Subarray
Maximum Product Subarray
You are given an array of integers. Your task is to compute the maximum product of a contiguous subarray. In other words, find the maximum value of \(\prod_{k=i}^{j} a_k\) for all valid intervals \(0 \le i \le j < n\), where \(a_k\) are the elements of the array.
For example, consider the array [2, 3, 4]
. The maximum product subarray is the entire array and its product is \(2 \times 3 \times 4 = 24\). Note that the array may contain negative numbers and zeros, which could change the sign and value of the product dramatically.
The input is given as follows: the first line contains a single integer representing the number of elements in the array, and the second line contains the list of integers separated by spaces.
Your output should be a single integer representing the maximum product achievable by any contiguous subarray.
inputFormat
The input consists of two lines:
- The first line contains an integer \(n\) (\(1 \le n \le 10^5\)), the number of elements in the array.
- The second line contains \(n\) space-separated integers \(a_1, a_2, \ldots, a_n\) where each \(a_i\) is in the range \(-10^4 \le a_i \le 10^4\).
outputFormat
Output a single integer which is the maximum product that can be obtained by any contiguous subarray of the given array.
## sample3
2 3 4
24