#K49862. Maximum Length Consecutive Subarray

    ID: 28737 Type: Default 1000ms 256MiB

Maximum Length Consecutive Subarray

Maximum Length Consecutive Subarray

You are given an array of integers, which may contain duplicates. Your task is to find the length of the longest subarray which, when its elements are rearranged, forms a consecutive sequence of integers. In other words, if you can rearrange the elements of the subarray such that they form a sequence \(a, a+1, a+2, \ldots\, a+k\), then the subarray is valid, and you need to output its length.

Note: The consecutive sequence does not need to appear in order in the array. Also, even if the array contains duplicates, consider only the unique numbers when checking for consecutiveness.

Example:

  • For input array [1, 2, 2, 5, 3], the longest consecutive subarray is [1, 2, 5, 3] which can be rearranged to [1, 2, 3, 5]. However, the longest sequence of consecutive numbers that can be formed is 1, 2, 3, hence the answer is 3.
  • For input array [10, 12, 11, 14, 13, 15], the entire array can be rearranged into a consecutive sequence from 10 to 15, so the answer is 6.

inputFormat

The first line of input contains a single integer \(N\) representing the number of elements in the array. The second line contains \(N\) space-separated integers representing the array elements.

outputFormat

Output a single integer which is the length of the maximum subarray that can be rearranged into a consecutive sequence.

## sample
5
1 2 2 5 3
3

</p>