#K81642. Maximum Product Subarray

    ID: 35799 Type: Default 1000ms 256MiB

Maximum Product Subarray

Maximum Product Subarray

You are given an integer array of size \( n \). Your task is to find a contiguous subarray within the array that has the largest product, and output that product.

Formally, given an array \( A = [a_1, a_2, \dots, a_n] \), find the maximum value of \( \prod_{i=l}^{r} a_i \) for \( 1 \le l \le r \le n \). The state update equations used in an efficient solution are:

[ \text{maxProd}(i) = \max(a_i, , a_i \times \text{maxProd}(i-1), , a_i \times \text{minProd}(i-1)) ]

[ \text{minProd}(i) = \min(a_i, , a_i \times \text{maxProd}(i-1), , a_i \times \text{minProd}(i-1)) ]

Note: The array may contain positive numbers, negative numbers, and zeros. You are required to implement a solution that reads input from standard input (stdin) and writes the result to standard output (stdout).

inputFormat

The input consists of two lines:

  1. The first line contains an integer \( n \) representing the number of elements in the array.
  2. The second line contains \( n \) integers separated by spaces, which are the elements of the array.

outputFormat

Output a single integer: the maximum product of a contiguous subarray of the given array.

## sample
4
2 3 -2 4
6