#C1530. Longest Contiguous Monotonic Subarray

    ID: 44746 Type: Default 1000ms 256MiB

Longest Contiguous Monotonic Subarray

Longest Contiguous Monotonic Subarray

Given a sequence of integers, your task is to find the length of the longest contiguous subarray in which the elements are either strictly increasing or strictly decreasing. Note that if two consecutive elements are equal, it breaks the monotonic trend and the count resets.

Formally, consider an array \(a_1, a_2, \dots, a_n\). You need to determine the maximum length \(L\) such that there exists an index \(i\) with a contiguous segment \(a_i, a_{i+1}, \dots, a_{i+L-1}\) satisfying one of the following conditions for every adjacent pair:

  • Strictly Increasing: \(a_j < a_{j+1}\) for all \(i \leq j < i+L-1\), or
  • Strictly Decreasing: \(a_j > a_{j+1}\) for all \(i \leq j < i+L-1\).

If the array is empty, the answer is 0. If it contains only one element, the answer is 1.

inputFormat

The first line contains an integer \(n\) (\(0 \leq n \leq 10^5\)), the number of elements in the array. If \(n > 0\), the second line contains \(n\) space-separated integers representing the array elements.

outputFormat

Output a single integer representing the length of the longest contiguous subarray that is strictly increasing or strictly decreasing.

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

</p>