#K1861. Longest Mountain in Array

    ID: 24609 Type: Default 1000ms 256MiB

Longest Mountain in Array

Longest Mountain in Array

Given an array of n integers, find the length of the longest contiguous subarray that forms a mountain.

A mountain is defined as a subarray where there exists an index i (\(1 \le i \le n-2\)) such that:

  • The subarray strictly increases from the left up to index i.
  • Then, it strictly decreases from index i to the right.

In other words, a subarray \(A[l..r]\) is a mountain if there exists an index \(i\) (\(l < i < r\)) such that:

\[ A[l] < A[l+1] < \cdots A[i+1] > \cdots > A[r] \]

Note that a mountain must have at least 3 elements. If no mountain exists, output 0.

inputFormat

The input is given via stdin and consists of two lines:

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

outputFormat

Output a single integer to stdout representing the length of the longest mountain subarray. If there is no mountain, output 0.

## sample
9
2 1 4 7 3 2 5 1 0
5