#C2943. Longest Contiguous Subsequence with Limited Range

    ID: 46315 Type: Default 1000ms 256MiB

Longest Contiguous Subsequence with Limited Range

Longest Contiguous Subsequence with Limited Range

Given an integer array, find the length of the longest contiguous subarray (segment) such that the difference between its maximum and minimum elements is at most \(1\). In other words, if the contiguous subarray has minimum value \(m\) and maximum value \(M\), then it should satisfy \(M - m \leq 1\).

The input consists of an integer \(n\) followed by \(n\) space-separated integers. If \(n = 0\), the array is considered empty and the answer is \(0\). The output should be a single integer representing the maximum length of such a contiguous segment.

inputFormat

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

outputFormat

Output a single integer that is the length of the longest contiguous subarray such that the difference between its maximum and minimum elements is at most \(1\).

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

</p>