#C5494. Longest Subarray with Difference One
Longest Subarray with Difference One
Longest Subarray with Difference One
You are given an array of integers. Your task is to find the length of the longest contiguous subarray such that the difference between the maximum and minimum values in the subarray is exactly 1.
In other words, if you consider any subarray \(a[l \ldots r]\), it is valid if and only if \(\max(a[l \ldots r]) - \min(a[l \ldots r]) = 1\). If there is no such subarray, output 0.
Note: The subarray must be contiguous and non-empty. The input is provided via standard input and the output via standard output.
inputFormat
The input consists of two lines:
- The first line contains a single integer \(n\) denoting the number of elements in the array.
- The second line contains \(n\) space-separated integers representing the elements of the array.
outputFormat
Output a single integer denoting the length of the longest contiguous subarray where the difference between the maximum and minimum element is exactly 1.
## sample8
1 2 2 1 3 4 2 1
4