#K57772. Longest Contiguous Subarray with Consecutive Numbers

    ID: 30495 Type: Default 1000ms 256MiB

Longest Contiguous Subarray with Consecutive Numbers

Longest Contiguous Subarray with Consecutive Numbers

Given an array of integers, find the length of the longest contiguous subarray such that its elements, when rearranged, form a sequence of consecutive numbers. In other words, for a subarray with minimum value \(min\) and maximum value \(max\), the subarray is valid if \(max - min = \text{length} - 1\) and no duplicate elements exist in the subarray.

For example, the subarray [10, 12, 11] is valid because when sorted it becomes [10, 11, 12] and \(12 - 10 = 2\) which is equal to \(3 - 1 = 2\). Your task is to determine the length of the longest such subarray.

inputFormat

The first line contains an integer (n) representing the number of elements in the array. The second line contains (n) space-separated integers representing the array elements.

outputFormat

Print a single integer representing the length of the longest contiguous subarray that forms a sequence of consecutive integers.## sample

3
10 12 11
3