#K83912. Longest Unimodal Subarray
Longest Unimodal Subarray
Longest Unimodal Subarray
You are given an array of n integers. Your task is to find the length of the longest unimodal subarray within the array. A unimodal subarray is defined as a contiguous segment of the array where the elements first strictly increase and then strictly decrease. In other words, if we denote the peak element by \(a_k\), then for the subarray \(a_i, a_{i+1}, \ldots, a_j\), there exists an index \(k\) with \(i < k < j\) such that:
[ a_i < a_{i+1} < \cdots < a_k \quad \text{and} \quad a_k > a_{k+1} > \cdots > a_j ]
If no such subarray exists, output 0.
Note: A valid unimodal subarray must have at least three elements.
inputFormat
The input is given from stdin in the following format:
n a1 a2 a3 ... an
Here, n
is the number of elements in the array, and the next line contains n
space-separated integers.
outputFormat
Output a single integer to stdout --- the length of the longest unimodal subarray. If there is no unimodal subarray, output 0.
## sample5
1 3 5 4 2
5