#K32817. Longest Mountain in Array
Longest Mountain in Array
Longest Mountain in Array
Given an array of integers, your task is to find the length of the longest mountain. A mountain is defined as a subarray that:
- Contains at least 3 elements.
- Strictly increases to a peak and then strictly decreases.
In other words, if we denote the indices of the mountain as \(L, L+1, \ldots, R\), then there exists an index \(i\) (with \(L < i < R\)) such that:
\[ nums[L] < nums[L+1] < \cdots nums[i+1] > \cdots > nums[R] \]
The length of the mountain is given by:
\[ \text{mountain_length} = R - L + 1 \]
If there is no mountain in the array, return 0
.
inputFormat
The input is read from standard input (stdin). The first line contains an integer T
representing the number of test cases. Each test case consists of two lines: the first line contains an integer N
denoting the number of elements in the array, and the second line contains N
space-separated integers representing the elements of the array.
For example:
4 7 2 1 4 7 3 2 5 3 2 2 2 3 0 1 0 11 2 1 4 7 3 2 5 8 6 3 2
outputFormat
For each test case, output the length of the longest mountain on a new line to standard output (stdout).
For the sample input, the correct output is:
5 0 3 6## sample
4
7
2 1 4 7 3 2 5
3
2 2 2
3
0 1 0
11
2 1 4 7 3 2 5 8 6 3 2
5
0
3
6
</p>