#C3141. Maximum Length Subarray with Near Elements
Maximum Length Subarray with Near Elements
Maximum Length Subarray with Near Elements
You are given an array of n integers. Your task is to find the length of the longest subarray (not necessarily contiguous in the original array) such that for any two elements in the subarray, the absolute difference between them is at most \(1\). For example, if the array is [1, 1, 2, 2, 3, 4], one of the optimal subarrays is [1, 1, 2, 2] which has length 4.
Note: The subarray in this context is defined as a collection of elements that can be selected from the array (order does not matter) provided they satisfy the given condition.
Constraints: \(0 \le n \le 10^5\); array elements can be any integer (including negative values).
inputFormat
The input is read from standard input. The first line contains a single integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated integers.
outputFormat
Output a single line to standard output containing the maximum length of a subarray where the absolute difference between any two elements is at most \(1\).
## sample6
1 1 2 2 3 4
4
</p>