#K73332. Longest Contiguous Subarray Length
Longest Contiguous Subarray Length
Longest Contiguous Subarray Length
Given an array of integers, determine the length of the longest contiguous subarray that forms a contiguous sequence when sorted. In other words, for a subarray a[i...j]
, if max(a[i...j]) - min(a[i...j]) = j - i
, then the elements of the subarray, when sorted, form a sequence of consecutive integers.
For example, consider the subarray [10, 12, 11]
. The minimum value is 10 and the maximum is 12, and since 12 - 10 = 2
and the number of indices between them is also 2 (i.e., j - i = 2
), this subarray forms a continuous sequence when sorted. Your task is to determine the maximum length among all such contiguous subarrays within the provided array.
The condition can be mathematically defined as: $$max(a[i\ldots j]) - min(a[i\ldots j]) = j - i$$ for a valid contiguous subarray.
inputFormat
The first line contains a single integer n
representing the number of elements in the array.
The second line contains n
space-separated integers denoting the elements of the array.
outputFormat
Output a single integer, the length of the longest contiguous subarray that forms a continuous sequence when sorted.
## sample3
10 12 11
3
</p>