#K15181. Longest Mountain Subarray

    ID: 24300 Type: Default 1000ms 256MiB

Longest Mountain Subarray

Longest Mountain Subarray

In this problem, you are given an array of integers and your task is to find the length of the longest subarray which forms a mountain. A mountain subarray is defined as a contiguous subarray that has a strictly increasing sequence followed by a strictly decreasing sequence, and it must contain at least three elements. More formally, a subarray a[l...r] (with r - l + 1 ≥ 3) is considered a mountain if there exists an index p (l < p < r) such that:

al<al+1<<apa_l < a_{l+1} < \cdots < a_p ap>ap+1>>ara_p > a_{p+1} > \cdots > a_r

If no such mountain exists, output 0. The input is read from standard input and the result should be printed to standard output.

inputFormat

The input is given via standard input. The first line contains a single integer n (the length of the array). The second line contains n space-separated integers representing the array.

outputFormat

Output a single integer representing the length of the longest mountain subarray. If no mountain exists, output 0.## sample

9
2 1 4 7 3 2 5 6 7
5